Symptom Comparisons

Row

One Test

Row

Two Tests - Test 1

Two Tests - Test 2

Row

Three Tests - Test 1

Three Tests - Test 2

Three Tests - Test 3

Row

Four Tests - Test 1

Four Tests - Test 2

Four Tests - Test 3

Four Tests - Test 4

Cluster Comparisons

Row

1 Test Cluster Summary

1 Test Cluster Boxplot

1 Test Cluster ANOVA

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   36.3   7.263   347.5 <2e-16 ***
Residuals       59940 1252.8   0.021                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

1 Test Cluster PWC

Row

2 Tests Cluster Summary - Test 1

2 Tests Cluster Boxplot - Test 1

2 Tests Cluster ANOVA - Test 1

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   33.3   6.652   264.1 <2e-16 ***
Residuals       31764  800.0   0.025                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2 Tests Cluster PWC - Test 1

2 Tests Cluster Summary - Test 2

2 Tests Cluster Boxplot - Test 2

2 Tests Cluster ANOVA - Test 2

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   5.38  1.0763   137.1 <2e-16 ***
Residuals       31764 249.40  0.0079                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2 Tests Cluster PWC - Test 2

Row

3 Tests Cluster Summary - Test 1

3 Tests Cluster Boxplot - Test 1

3 Tests Cluster ANOVA - Test 1

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   20.0   4.005   132.8 <2e-16 ***
Residuals       13362  402.9   0.030                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3 Tests Cluster PWC - Test 1

3 Tests Cluster Summary - Test 2

3 Tests Cluster Boxplot - Test 2

3 Tests Cluster ANOVA - Test 2

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   4.52  0.9040   78.46 <2e-16 ***
Residuals       13362 153.95  0.0115                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3 Tests Cluster PWC - Test 2

3 Tests Cluster Summary - Test 3

3 Tests Cluster Boxplot - Test 3

3 Tests Cluster ANOVA - Test 3

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   0.55 0.11074   24.04 <2e-16 ***
Residuals       13362  61.55 0.00461                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3 Tests Cluster PWC - Test 3

Row

4 Tests Cluster Summary - Test 1

4 Tests Cluster Boxplot - Test 1

4 Tests Cluster ANOVA - Test 1

                  Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster    5    9.9  1.9802   48.72 <2e-16 ***
Residuals       4674  190.0  0.0406                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Tests Cluster PWC - Test 1

4 Tests Cluster Summary - Test 2

4 Tests Cluster Boxplot - Test 2

4 Tests Cluster ANOVA - Test 2

                  Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster    5   6.01  1.2015   59.77 <2e-16 ***
Residuals       4674  93.95  0.0201                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Tests Cluster PWC - Test 2

4 Tests Cluster Summary - Test 3

4 Tests Cluster Boxplot - Test 3

4 Tests Cluster ANOVA - Test 3

                  Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster    5   0.96 0.19257   19.81 <2e-16 ***
Residuals       4674  45.43 0.00972                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Tests Cluster PWC - Test 3

4 Tests Cluster Summary - Test 4

4 Tests Cluster Boxplot - Test 4

4 Tests Cluster ANOVA - Test 4

                  Df Sum Sq Mean Sq F value   Pr(>F)    
symptom_cluster    5   0.25 0.05030   6.606 3.89e-06 ***
Residuals       4674  35.59 0.00761                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Tests Cluster PWC - Test 4

Gender-Cluster Interaction

Row

1 Test Cluster Summary

Non-Normalized Summary

1 Test Cluster Boxplot

1 Test Cluster Two-Way ANOVA

1 Test Cluster PWC

Row

2 Tests Cluster Summary - Test 1

Non-Normalized Summary

2 Tests Cluster Boxplot - Test 1

2 Tests Cluster Two-Way ANOVA - Test 1

2 Tests Cluster PWC - Test 1

2 Tests Cluster Summary - Test 2

2 Tests Cluster Boxplot - Test 2

2 Tests Cluster Two-Way ANOVA - Test 2

2 Tests Cluster PWC - Test 1

Row

3 Tests Cluster Summary - Test 1

3 Tests Cluster Boxplot - Test 1

3 Tests Cluster Two-Way ANOVA - Test 1

3 Tests Cluster PWC - Test 1

3 Tests Cluster Summary - Test 2

3 Tests Cluster Boxplot - Test 2

3 Tests Cluster Two-Way ANOVA - Test 2

3 Tests Cluster PWC - Test 2

3 Tests Cluster Summary - Test 3

3 Tests Cluster Boxplot - Test 3

3 Tests Cluster Two-Way ANOVA - Test 3

3 Tests Cluster PWC - Test 3

Row

4 Tests Cluster Summary - Test 1

4 Tests Cluster Boxplot - Test 1

4 Tests Cluster Two-Way ANOVA - Test 1

4 Tests Cluster Interaction PWC - Test 1

4 Tests - Test 1 One-Way ANOVA Main Effect Cluster

4 Tests - Test 1 One-Way ANOVA Main Effect Cluster PWC

4 Tests - Test 1 One-Way ANOVA Main Effect Gender

4 Tests - Test 1 One-Way ANOVA Main Effect Gender PWC

4 Tests Cluster Summary - Test 2

4 Tests Cluster Boxplot - Test 2

4 Tests Cluster Two-Way ANOVA - Test 2

4 Tests Cluster PWC - Test 2

4 Tests Cluster Summary - Test 3

4 Tests Cluster Boxplot - Test 3

4 Tests Cluster Two-Way ANOVA - Test 3

4 Tests Cluster PWC - Test 3

4 Tests Cluster Summary - Test 4

4 Tests Cluster Boxplot - Test 4

4 Tests Cluster Two-Way ANOVA - Test 4

4 Tests Cluster PWC - Test 4

Test 1 Symptom Comparison

Row

Total Symptom Score Summary

Total Symptom Score Summary - Gender

Total Symptom Score Summary - Number of Tests Completed

Total Symptom Score Boxplot

Row

Total Symptom Score Two-Way ANOVA

Total Symptom Score Two-Way ANOVA Interaction PWC

Total Symptom Score One-Way ANOVA Main Effect Gender

Total Symptom Score One-Way ANOVA Main Effect Gender PWC

Total Symptom Score One-Way ANOVA Main Effect Number of Tests Completed

Total Symptom Score One-Way ANOVA Main Effect Number of Tests Completed PWC

Time

Row

Two Tests - Time Between Tests 1/2

Two Tests - Time Between Tests 1/2

Row

Three Tests - Time Between Tests 1/2

Three Tests - Time Between Tests 1/2

Three Tests - Time Between Tests 1/3

Three Tests - Time Between Tests 1/3

Three Tests - Time Between Tests 2/3

Three Tests - Time Between Tests 2/3

Row

Four Tests - Time Between Tests 1/2

Four Tests - Time Between Tests 1/2

Four Tests - Time Between Tests 2/3

Four Tests - Time Between Tests 2/3

Four Tests - Time Between Tests 3/4

Four Tests - Time Between Tests 3/4

Four Tests - Time Between Tests 1/4

Four Tests - Time Between Tests 1/4

Rate

Row

Two Tests Percent Decrease

Row

Three Tests Percent Decrease

Row

Four Tests Percent Decrease

Row

Two Tests Non-Normalized Test 1 Summary

Two Tests Non-Normalized Test 2 Summary

Row

Three Tests Non-Normalized Test 1 Summary

Three Tests Non-Normalized Test 2 Summary

Three Tests Non-Normalized Test 3 Summary

Row

Four Tests Non-Normalized Test 1 Summary

Four Tests Non-Normalized Test 2 Summary

Four Tests Non-Normalized Test 3 Summary

Four Tests Non-Normalized Test 4 Summary

---
title: "HCAMP Cluster Comparison"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
    vertical_layout: scroll
    theme: united
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(here)
library(janitor)
library(rio)
library(colorblindr)
library(gghighlight)
library(forcats)
library(ggrepel)
library(knitr)
library(kableExtra)
library(reactable)
library(plotly)
library(glue)
library(fs)
library(rstatix)
library(ggpubr)
library(writexl) 
library(remotes)
library(profvis) 


theme_fivethirtyeight <- function(base_size = 15, base_family = "") {
  theme_grey(base_size = base_size, base_family = base_family) %+replace%
    theme(
      
      # Base elements which are not used directly but inherited by others
      line =              element_line(colour = '#DADADA', size = 0.75, 
                                       linetype = 1, lineend = "butt"),
      rect =              element_rect(fill = "#F0F0F0", colour = "#F0F0F0", 
                                       size = 0.5, linetype = 1),
      text =              element_text(family = base_family, face = "plain",
                                       colour = "#656565", size = base_size,
                                       hjust = 0.5, vjust = 0.5, angle = 0, 
                                       lineheight = 0.9),
      
      # Modified inheritance structure of text element
      plot.title =        element_text(size = rel(1.5), family = '' , 
                                       face = 'bold', hjust = -0.05, 
                                       vjust = 1.5, colour = '#3B3B3B'),
      axis.title.x =      element_blank(),
      axis.title.y =      element_blank(),
      axis.text =         element_text(),
      
      # Modified inheritance structure of line element
      axis.ticks =        element_line(),
      panel.grid.major =  element_line(),
      panel.grid.minor =  element_blank(),
      
      # Modified inheritance structure of rect element
      plot.background =   element_rect(),
      panel.background =  element_rect(),
      legend.key =        element_rect(colour = '#DADADA'),
      
      # Modifiying legend.position
      legend.position = 'none',
      
      complete = TRUE
    )
}


theme_set(theme_fivethirtyeight())

# theme_set(theme_minimal(15) +
#             theme(legend.position = "bottom",
#                   panel.grid.major.x = element_line(color = "gray60"),
#                   panel.grid.minor.x = element_blank(),
#                   panel.grid.major.y = element_blank()) 
#           )

```

```{r global, include=FALSE}
#all clean sims data
sims_concussion_data <- read_csv(here("data", "sims_concussion_data.csv"))

sims_concussion_data <- sims_concussion_data %>% 
  mutate(age = as.factor(age))

#impact data only

one_i_test <- read_csv(here("data", "one_impact_pcss_test.csv"))

one_impact_test <- one_i_test %>% 
  select(-c(5, 6))

two_i_test <- read_csv(here("data", "two_impact_pcss_tests.csv"))

two_impact_test <- two_i_test %>% 
  select(-c(6, 7))

three_i_test <- read_csv(here("data", "three_impact_pcss_tests.csv"))

three_impact_test <- three_i_test %>% 
  select(-c(7, 8))

four_i_test <- read_csv(here("data", "four_impact_pcss_tests.csv"))

four_impact_test <- four_i_test %>% 
  select(-c(8, 9))

# impact sims merge data

one_impact_sims <- read_csv(here("data", "one_post_injury_test_impact_sims_merge.csv"))

two_impact_sims <- read_csv(here("data", "two_post_injury_test_impact_sims_merge.csv"))

three_impact_sims <- read_csv(here("data", "three_post_injury_test_impact_sims_merge.csv"))

four_impact_sims <- read_csv(here("data", "four_post_injury_test_impact_sims_merge.csv"))

baseline_dates <- read_csv(here("data", "baseline_dates.csv"))

# pcss items 

pcss_items <- import(here("data", "pcss_items.xlsx"),
               setclass = "tbl_df") 

pcss_items_data <- import(here("data", "pcss_items_data.xlsx"),
               setclass = "tbl_df") 

pcss_cluster <- import(here("data", "cluster_scores.xlsx"),
               setclass = "tbl_df") 


```

# Symptom Comparisons

Sidebar {.sidebar}
------------

Symptoms that appear to be rated with higher severity more consistently: 

  1. Headache 
  2. Difficulty concentrating 
  3. Difficulty remembering 
  4. Irritability 
  5. Light sensitivity 
  
Symptoms from the headache-migraine, cognitive and anxiety-mood clusters appear to remain elevated throughout recovery. 

The vestibular cluster symptom of dizziness appears to be rated highest at the time of first testing and lessen in severity across the recovery time. 

```{r, include=FALSE}
one_impact_test_cluster <- one_impact_test %>% 
  pivot_longer(
    cols = c(32:37),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) %>% 
  select(c(1:8, 37, 38))

one_impact_test_symptoms <- one_impact_test %>% 
  pivot_longer(
    cols = c(9:30),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_1_rating",
    names_pattern = "(.*)_post_injury_1"
  ) %>% 
  select(c(1:15, 21, 22))

tst_aov <- aov(total_symptom_score_post_injury_1 ~ post_injury_1_rating,
               data = one_impact_test_symptoms)

summary(tst_aov)

one_impact_test_symptoms <- left_join(one_impact_test_symptoms, pcss_items_data)

```

```{r, include=FALSE}

two_impact_test_1 <- two_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_1"))

two_impact_test_2 <- two_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_2"))

two_impact_test_1_symptoms <- two_impact_test_1 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_1_rating",
    names_pattern = "(.*)_post_injury_1"
  ) %>% 
  select(-c(14:18))

two_impact_test_1_symptoms <- left_join(two_impact_test_1_symptoms, pcss_items_data)

two_impact_test_2_symptoms <- two_impact_test_2 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_2_rating",
    names_pattern = "(.*)_post_injury_2"
  ) %>% 
  select(-c(14:18))

two_impact_test_2_symptoms <- left_join(two_impact_test_2_symptoms, pcss_items_data)

```

```{r, include=FALSE}
three_impact_test_1 <- three_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_1"))

three_impact_test_2 <- three_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_2"))

three_impact_test_3 <- three_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_3"))

three_impact_test_1_symptoms <- three_impact_test_1 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_1_rating",
    names_pattern = "(.*)_post_injury_1"
  ) %>% 
  select(-c(14:18))

three_impact_test_1_symptoms <- left_join(three_impact_test_1_symptoms, pcss_items_data)


three_impact_test_2_symptoms <- three_impact_test_2 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_2_rating",
    names_pattern = "(.*)_post_injury_2"
  ) %>% 
  select(-c(14:18))

three_impact_test_2_symptoms <- left_join(three_impact_test_2_symptoms, pcss_items_data)

three_impact_test_3_symptoms <- three_impact_test_3 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_3_rating",
    names_pattern = "(.*)_post_injury_3"
  ) %>% 
  select(-c(14:18))

three_impact_test_3_symptoms <- left_join(three_impact_test_3_symptoms, pcss_items_data)

```

```{r, include=FALSE}
four_impact_test_1 <- four_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_1"))

four_impact_test_2 <- four_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_2"))

four_impact_test_3 <- four_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_3"))

four_impact_test_4 <- four_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_4"))

four_impact_test_1_symptoms <- four_impact_test_1 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_1_rating",
    names_pattern = "(.*)_post_injury_1"
  ) %>% 
  select(-c(14:18))

four_impact_test_1_symptoms <- left_join(four_impact_test_1_symptoms, pcss_items_data)


four_impact_test_2_symptoms <- four_impact_test_2 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_2_rating",
    names_pattern = "(.*)_post_injury_2"
  ) %>% 
  select(-c(14:18))

four_impact_test_2_symptoms <- left_join(four_impact_test_2_symptoms, pcss_items_data)

four_impact_test_3_symptoms <- four_impact_test_3 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_3_rating",
    names_pattern = "(.*)_post_injury_3"
  ) %>% 
  select(-c(14:18))

four_impact_test_3_symptoms <- left_join(four_impact_test_3_symptoms, pcss_items_data)


four_impact_test_4_symptoms <- four_impact_test_4 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_4_rating",
    names_pattern = "(.*)_post_injury_4"
  ) %>% 
  select(-c(14:18))

four_impact_test_4_symptoms <- left_join(four_impact_test_4_symptoms, pcss_items_data)
```


Row {.tabset}
-----------------------------------------------------------------------

### One Test 

```{r, include=FALSE}
all_symptom_tbl <- one_impact_test_symptoms %>% 
    group_by(gender, cluster, symptom) %>% 
    summarize(Mean = mean(post_injury_1_rating),
              SD = sd(post_injury_1_rating),
              Min = min(post_injury_1_rating),
              Max = max(post_injury_1_rating),
              Total = length(post_injury_1_rating)) %>% 
    mutate(Mean = as.numeric(Mean),
           SD = as.numeric(SD),
           Min = as.numeric(Min),
           Max = as.numeric(Max)) %>% 
    arrange(desc(Mean)) %>% 
    mutate_if(is.numeric, round, 2) %>% 
    reactable(
      defaultColDef = colDef(align = "center"),
      columns = list(
        gender = colDef(name = "Gender"),
        cluster = colDef(name = "Symptom Cluster"),
        symptom = colDef(name = "Symptom"),
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE)),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )

symptom_tbl <- function(df, a, b, c, d) {
    df %>% 
    group_by({{a}}, {{b}}, {{c}}) %>% 
    summarize(Mean = mean({{d}}),
              SD = sd({{d}}),
              Min = min({{d}}),
              Max = max({{d}}),
              Total = length({{d}})) %>% 
    mutate(Mean = as.numeric(Mean),
           SD = as.numeric(SD),
           Min = as.numeric(Min),
           Max = as.numeric(Max)) %>% 
    arrange(desc(Mean)) %>% 
    mutate_if(is.numeric, round, 2) %>% 
    reactable(
      defaultColDef = colDef(align = "center"),
      columns = list(
        gender = colDef(name = "Gender"),
        cluster = colDef(name = "Symptom Cluster"),
        symptom = colDef(name = "Symptom"),
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE)),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )
}

symptom_tbl_over_three <- function(df, a, b, c, d) {
    df %>% 
    group_by({{a}}, {{b}}, {{c}}) %>% 
    filter({{d}} >=3) %>% 
    summarize(Mean = mean({{d}}),
              SD = sd({{d}}),
              Min = min({{d}}),
              Max = max({{d}}),
              Total = length({{d}})) %>% 
    mutate(Mean = as.numeric(Mean),
           SD = as.numeric(SD),
           Min = as.numeric(Min),
           Max = as.numeric(Max)) %>% 
    arrange(desc(Mean)) %>% 
    mutate_if(is.numeric, round, 2) %>% 
    reactable(
      defaultColDef = colDef(align = "center"),
      columns = list(
        gender = colDef(name = "Gender"),
        cluster = colDef(name = "Symptom Cluster"),
        symptom = colDef(name = "Symptom"),
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE)),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )
}

```


```{r, include=TRUE}
symptom_tbl(one_impact_test_symptoms, gender, cluster, symptom, post_injury_1_rating)
```


Row {.tabset}
-----------------------------------------------------------------------

### Two Tests - Test 1

```{r, include=TRUE}
symptom_tbl(two_impact_test_1_symptoms, gender, cluster, symptom, post_injury_1_rating)
```

### Two Tests - Test 2 

```{r, include=TRUE}
symptom_tbl(two_impact_test_2_symptoms, gender, cluster, symptom, post_injury_2_rating)
```


Row {.tabset}
-----------------------------------------------------------------------

### Three Tests - Test 1

```{r, include=TRUE}
symptom_tbl(three_impact_test_1_symptoms, gender, cluster, symptom, post_injury_1_rating)
```

### Three Tests - Test 2 

```{r, include=TRUE}
symptom_tbl(three_impact_test_2_symptoms, gender, cluster, symptom, post_injury_2_rating)
```

### Three Tests - Test 3 

```{r, include=TRUE}
symptom_tbl(three_impact_test_3_symptoms, gender, cluster, symptom, post_injury_3_rating)
```



Row {.tabset}
-----------------------------------------------------------------------

### Four Tests - Test 1

```{r, include=TRUE}
symptom_tbl(four_impact_test_1_symptoms, gender, cluster, symptom, post_injury_1_rating)
```

### Four Tests - Test 2 

```{r, include=TRUE}
symptom_tbl(four_impact_test_2_symptoms, gender, cluster, symptom, post_injury_2_rating)
```

### Four Tests - Test 3

```{r, include=TRUE}
symptom_tbl(four_impact_test_3_symptoms, gender, cluster, symptom, post_injury_3_rating)
```

### Four Tests - Test 4

```{r, include=TRUE}
symptom_tbl(four_impact_test_4_symptoms, gender, cluster, symptom, post_injury_4_rating)
```

# Cluster Comparisons 

Sidebar {.sidebar}
------------

Symptom Cluster Potential Symptom Severity Range:

  1. Headache-Migraine Cluster: 0 - 24 (4 symptoms)
  2. Cognitive Cluster: 0 - 24 (4 symptoms)
  3. Anxiety-Mood Cluster: 0 - 24 (4 symptoms)
  4. Ocular-Motor Cluster: 0 - 6 (1 symptom)
  5. Vestibular Cluster: 0 - 24 (4 symptoms)
  6. Sleep Cluster: 0 - 30 (5 symptoms)
  

To compare symptom cluster severity scores, each cluster was rescaled for the distribution of scores to fall between 0 to 1. Each cluster was rescaled individually and then grouped together into the same variable corresponding to post-injury test to compare with one-way ANOVAs. 

**One Test Summary**

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor and cognitive/sleep
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters
  * Cognitive and sleep clusters tied for second highest rated clusters
  * Anxiety-mood and ocular-motor rated the lowest 


**Two Tests Summary**

_Test One:_

  * Non-significant cluster comparisons: cognitive/sleep 
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters 
  * Cognitive and sleep clusters tied for second highest rated clusters 
  * Anxiety-mood and ocular-motor rated the lowest 

_Test Two:_

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor, cognitive/sleep, anxiety-mood/vestibular, and ocular-motor/vestibular
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters 

**Three Tests Summary**

_Test One:_

  * Non-significant cluster comparisons: anxiety-mood/vestibular 
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters

_Test Two:_

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor, cognitive/sleep, anxiety-mood/vestibular, and ocular-motor/vestibular
  * Headache-migraine cluster significantly higher than all other clusters 
  
_Test Three:_

  * Non-significant cluster comparisons: cognitive/headache-migraine, anxiety-mood/ocular-motor, cognitive/sleep, headache-migraine/sleep, anxiety-mood/vestibular, and ocular-motor/vestibular
  * Headache-migraine, cognitive, and sleep clusters significantly higher than other clusters 

**Four Tests Summary**

_Test One:_

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor, cognitive/sleep, cognitive/vestibular, and sleep/vestibular
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters 
  * Cognitive, vestibular, and sleep clusters not significantly different than each other and represent second set of clusters behind headache-migraine 
  
_Test Two:_

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor, cognitive/sleep, and ocular-motor/vestibular
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters 

_Test Three:_ 

  * Non-significant cluster comparisons: cognitive/headache-migraine, anxiety-mood/ocular-motor, cognitive/sleep, anxiety-mood/vestibular, and ocular-motor/vestibular 
  * All other PWC were significant 
  * Headache-migraine cluster rated the highest but not significantly different than cognitive cluster 
  * Cognitive cluster rated second highest but not significantly different than sleep cluster 
_Test Four:_

  * Non-significant cluster comparisons: anxiety-mood/cognitive, cognitive/headache-migraine, anxiety-mood/ocular-motor, cognitive/ocular-motor, anxiety-mood/sleep, cognitive/sleep, headache-migraine/sleep, ocular-motor/sleep, anxiety-mood/vestibular, ocular-motor/vestibular, and sleep/vestibular 
  * Significant cluster comparisons: cognitive/vestibular, headache-migraine/vestibular, anxiety-mood/headache-migraine, ocular-motor/headache-migraine
  * Probably most important comparison from this test was the significant difference between anxiety-mood and headache-migraine severity ratings 
  
Row {.tabset}
-----------------------------------------------------------------------

### 1 Test Cluster Summary 

```{r, include=FALSE}
clusters_one_impact_test <- one_impact_test %>% 
  select(c(1:8, 32:37)) %>% 
  mutate(#X1 = as.character(X1),
         row = as.character(row),
         age = as.character(age))

str(clusters_one_impact_test)

# normalize <- function(x) {
# return ((x - min(x)) / (max(x) - min(x)))
# }

rescale01b <- function(x) {
  if(is.numeric(x)) {
    z <- na.omit(x)
    min_z <- min(z)
    max_z <- max(z)
    (z - min_z) / (max_z - min_z)
  }
  else {
    return()
  }
}

rescale01b(c(0, 5, 10))

clusters_one_impact_test_scaled <- map_df(clusters_one_impact_test, rescale01b)

clusters_one_impact_test_scaled <- bind_cols(clusters_one_impact_test, 
                                             clusters_one_impact_test_scaled)

clusters_one_impact_test_scaled <- clusters_one_impact_test_scaled %>% 
  select(-c(9:14))


clusters_one_impact_test_scaled[sapply(clusters_one_impact_test_scaled, is.character)] <- lapply(clusters_one_impact_test_scaled[sapply(clusters_one_impact_test_scaled, is.character)], as.factor)

str(clusters_one_impact_test_scaled)

clusters_one_impact_test_scaled_long <- clusters_one_impact_test_scaled %>% 
    pivot_longer(
    cols = c(10:15),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 

clusters_one_impact_test_long <- clusters_one_impact_test %>% 
    pivot_longer(
    cols = c(9:14),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 

str(clusters_one_impact_test_scaled_long)
```

```{r, include=FALSE}
#summary 
cluster_smry_tbl <- function(df, x) {
  df %>% 
  group_by(symptom_cluster) %>% 
  summarize(mean = mean({{x}}),
            sd = sd({{x}}),
            min = min({{x}}),
            max = max({{x}}),
            total = length({{x}})) %>%
  mutate(mean = as.numeric(mean),
         sd = as.numeric(sd),
         min = as.numeric(min),
         max = as.numeric(max),
         total = as.numeric(total)) %>% 
  reactable(
    columns = list(
      symptom_cluster = colDef(name = "Symptom Cluster",
                            align = "center"),
      mean = colDef(name = "Mean",
                    align = "center",
                    format = colFormat(digits = 2, separators = TRUE)),
      sd = colDef(name = "SD",
                  align = "center",
                  format = colFormat(digits = 2, separators = TRUE)),
      min = colDef(name = "Min",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      max = colDef(name = "Max",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      total = colDef(name = "Total",
                     align = "center",
                   format = colFormat(separators = TRUE, suffix = " concussions"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
  )
}

cluster_smry_tbl(clusters_one_impact_test_scaled_long, post_injury_1_score)
```

```{r, include=TRUE}
cluster_smry_tbl(clusters_one_impact_test_scaled_long, post_injury_1_score)
```

### 1 Test Cluster Boxplot 

```{r, include=FALSE}
cluster_bxp_1 <- ggplot(clusters_one_impact_test_scaled_long,
                      aes(symptom_cluster, post_injury_1_score)) +
  geom_boxplot() + 
  labs(x = "Symptom Cluster",
       y = "Scaled Severity Score") +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 25))

cluster_bxp <- function(df, x, y) {
  ggplot(df, aes({{x}}, {{y}})) +
  geom_boxplot() + 
  labs(x = "Symptom Cluster",
       y = "Scaled Severity Score") +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 25))
}

cluster_bxp(clusters_one_impact_test_scaled_long, symptom_cluster, post_injury_1_score)

```

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_one_impact_test_scaled_long, symptom_cluster, post_injury_1_score))
```

### 1 Test Cluster ANOVA 

```{r, include=FALSE}
cluster_one_test_aov <- aov(post_injury_1_score ~ symptom_cluster, 
                            data = clusters_one_impact_test_scaled_long)

summary(cluster_one_test_aov)

cluster_one_test_aov
```

```{r, include=TRUE}
summary(cluster_one_test_aov)
```

### 1 Test Cluster PWC

```{r, include=FALSE}
cluster_one_test_aov_pwc <- clusters_one_impact_test_scaled_long %>% 
  pairwise_t_test(
    post_injury_1_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_one_test_aov_pwc <- cluster_one_test_aov_pwc %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_one_test_aov_pwc %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


Row {.tabset}
-----------------------------------------------------------------------

### 2 Tests Cluster Summary - Test 1

```{r, include=FALSE}

clusters_two_impact_test_1 <- two_impact_test %>% 
  select(c(1:12, contains("_post_injury_1"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2))

clusters_two_impact_test_1 <- clusters_two_impact_test_1 %>% 
  select(c(1:12, 36:41))

clusters_two_impact_test_1_scaled <- map_df(clusters_two_impact_test_1, rescale01b)

clusters_two_impact_test_1_scaled <- bind_cols(clusters_two_impact_test_1, 
                                             clusters_two_impact_test_1_scaled)

clusters_two_impact_test_1_scaled <- clusters_two_impact_test_1_scaled %>% 
  select(-c(13:18))


clusters_two_impact_test_1_scaled[sapply(clusters_two_impact_test_1_scaled, is.character)] <- lapply(clusters_two_impact_test_1_scaled[sapply(clusters_two_impact_test_1_scaled, is.character)], as.factor)

str(clusters_two_impact_test_1_scaled)

clusters_two_impact_test_1_long <- clusters_two_impact_test_1_scaled %>% 
    pivot_longer(
    cols = c(14:19),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 


clusters_two_impact_test_2 <- two_impact_test %>% 
  select(c(1:12, contains("_post_injury_2"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2))

clusters_two_impact_test_2 <- clusters_two_impact_test_2 %>% 
  select(c(1:12, 36:41))

clusters_two_impact_test_2_scaled <- map_df(clusters_two_impact_test_2, rescale01b)

clusters_two_impact_test_2_scaled <- bind_cols(clusters_two_impact_test_2, 
                                             clusters_two_impact_test_2_scaled)

clusters_two_impact_test_2_scaled <- clusters_two_impact_test_2_scaled %>% 
  select(-c(13:18))


clusters_two_impact_test_2_scaled[sapply(clusters_two_impact_test_2_scaled, is.character)] <- lapply(clusters_two_impact_test_2_scaled[sapply(clusters_two_impact_test_2_scaled, is.character)], as.factor)

str(clusters_two_impact_test_2_scaled)

clusters_two_impact_test_2_long <- clusters_two_impact_test_2_scaled %>% 
    pivot_longer(
    cols = c(14:19),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  ) 

clusters_two_impact_test_complete <- left_join(clusters_two_impact_test_1_long,
                                               clusters_two_impact_test_2_long)
```

```{r, include=TRUE}
cluster_smry_tbl(clusters_two_impact_test_complete, post_injury_1_score)
```

### 2 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_two_impact_test_complete, 
                     symptom_cluster, post_injury_1_score))
```

### 2 Tests Cluster ANOVA - Test 1

```{r, include=FALSE}
cluster_two_tests_aov_1 <- aov(post_injury_1_score ~ symptom_cluster, 
                            data = clusters_two_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_two_tests_aov_1)
```

### 2 Tests Cluster PWC - Test 1

```{r, include=FALSE}
cluster_two_tests_aov_pwc_1 <- clusters_two_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_1_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_two_tests_aov_pwc_1 <- cluster_two_tests_aov_pwc_1 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_two_tests_aov_pwc_1 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


### 2 Tests Cluster Summary - Test 2

```{r, include=TRUE}
cluster_smry_tbl(clusters_two_impact_test_complete, post_injury_2_score)
```

### 2 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_two_impact_test_complete, 
                     symptom_cluster, post_injury_2_score))
```

### 2 Tests Cluster ANOVA - Test 2

```{r, include=FALSE}
cluster_two_tests_aov_2 <- aov(post_injury_2_score ~ symptom_cluster, 
                            data = clusters_two_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_two_tests_aov_2)
```

### 2 Tests Cluster PWC - Test 2

```{r, include=FALSE}
cluster_two_tests_aov_pwc_2 <- clusters_two_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_2_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_two_tests_aov_pwc_2 <- cluster_two_tests_aov_pwc_2 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_two_tests_aov_pwc_2 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```



Row {.tabset}
-----------------------------------------------------------------------

### 3 Tests Cluster Summary - Test 1

```{r, include=FALSE}

clusters_three_impact_test_1 <- three_impact_test %>% 
  select(c(1:16, contains("_post_injury_1"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3))

clusters_three_impact_test_1 <- clusters_three_impact_test_1 %>% 
  select(c(1:16, 40:45))

clusters_three_impact_test_1_scaled <- map_df(clusters_three_impact_test_1, rescale01b)

clusters_three_impact_test_1_scaled <- bind_cols(clusters_three_impact_test_1, 
                                             clusters_three_impact_test_1_scaled)

clusters_three_impact_test_1_scaled <- clusters_three_impact_test_1_scaled %>% 
  select(-c(17:22))

clusters_three_impact_test_1_scaled[sapply(clusters_three_impact_test_1_scaled, is.character)] <- lapply(clusters_three_impact_test_1_scaled[sapply(clusters_three_impact_test_1_scaled, is.character)], as.factor)

str(clusters_three_impact_test_1_scaled)

clusters_three_impact_test_1_long <- clusters_three_impact_test_1_scaled %>% 
    pivot_longer(
    cols = c(18:23),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 


clusters_three_impact_test_2 <- three_impact_test %>% 
  select(c(1:16, contains("_post_injury_2"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3))

clusters_three_impact_test_2 <- clusters_three_impact_test_2 %>% 
  select(c(1:16, 40:45))

clusters_three_impact_test_2_scaled <- map_df(clusters_three_impact_test_2, rescale01b)

clusters_three_impact_test_2_scaled <- bind_cols(clusters_three_impact_test_2, 
                                             clusters_three_impact_test_2_scaled)

clusters_three_impact_test_2_scaled <- clusters_three_impact_test_2_scaled %>% 
  select(-c(17:22))

clusters_three_impact_test_2_scaled[sapply(clusters_three_impact_test_2_scaled, is.character)] <- lapply(clusters_three_impact_test_2_scaled[sapply(clusters_three_impact_test_2_scaled, is.character)], as.factor)

str(clusters_three_impact_test_2_scaled)

clusters_three_impact_test_2_long <- clusters_three_impact_test_2_scaled %>% 
    pivot_longer(
    cols = c(18:23),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  ) 


clusters_three_impact_test_3 <- three_impact_test %>% 
  select(c(1:16, contains("_post_injury_3"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3))

clusters_three_impact_test_3 <- clusters_three_impact_test_3 %>% 
  select(c(1:16, 40:45))

clusters_three_impact_test_3_scaled <- map_df(clusters_three_impact_test_3, rescale01b)

clusters_three_impact_test_3_scaled <- bind_cols(clusters_three_impact_test_3, 
                                             clusters_three_impact_test_3_scaled)

clusters_three_impact_test_3_scaled <- clusters_three_impact_test_3_scaled %>% 
  select(-c(17:22))

clusters_three_impact_test_3_scaled[sapply(clusters_three_impact_test_3_scaled, is.character)] <- lapply(clusters_three_impact_test_3_scaled[sapply(clusters_three_impact_test_3_scaled, is.character)], as.factor)

str(clusters_three_impact_test_3_scaled)

clusters_three_impact_test_3_long <- clusters_three_impact_test_3_scaled %>% 
    pivot_longer(
    cols = c(18:23),
    names_to = "symptom_cluster",
    values_to = "post_injury_3_score",
    names_pattern = "(.*)_score_post_injury_3"
  ) 

clusters_three_impact_test_join_1 <- left_join(clusters_three_impact_test_1_long,
                                               clusters_three_impact_test_2_long)

clusters_three_impact_test_complete <- left_join(clusters_three_impact_test_join_1, 
                                                 clusters_three_impact_test_3_long)
```


```{r, include=TRUE}
cluster_smry_tbl(clusters_three_impact_test_complete, post_injury_1_score)
```

### 3 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_three_impact_test_complete, 
                     symptom_cluster, post_injury_1_score))
```

### 3 Tests Cluster ANOVA - Test 1

```{r, include=FALSE}
cluster_three_tests_aov_1 <- aov(post_injury_1_score ~ symptom_cluster, 
                            data = clusters_three_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_three_tests_aov_1)
```

### 3 Tests Cluster PWC - Test 1

```{r, include=FALSE}
cluster_three_tests_aov_pwc_1 <- clusters_three_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_1_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_three_tests_aov_pwc_1 <- cluster_three_tests_aov_pwc_1 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_three_tests_aov_pwc_1 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


### 3 Tests Cluster Summary - Test 2

```{r, include=TRUE}
cluster_smry_tbl(clusters_three_impact_test_complete, post_injury_2_score)
```

### 3 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_three_impact_test_complete, 
                     symptom_cluster, post_injury_2_score))
```

### 3 Tests Cluster ANOVA - Test 2

```{r, include=FALSE}
cluster_three_tests_aov_2 <- aov(post_injury_2_score ~ symptom_cluster, 
                            data = clusters_three_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_three_tests_aov_2)
```

### 3 Tests Cluster PWC - Test 2

```{r, include=FALSE}
cluster_three_tests_aov_pwc_2 <- clusters_three_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_2_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_three_tests_aov_pwc_2 <- cluster_three_tests_aov_pwc_2 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_three_tests_aov_pwc_2 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


### 3 Tests Cluster Summary - Test 3

```{r, include=TRUE}
cluster_smry_tbl(clusters_three_impact_test_complete, post_injury_3_score)
```

### 3 Tests Cluster Boxplot - Test 3

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_three_impact_test_complete, 
                     symptom_cluster, post_injury_3_score))
```

### 3 Tests Cluster ANOVA - Test 3

```{r, include=FALSE}
cluster_three_tests_aov_3 <- aov(post_injury_3_score ~ symptom_cluster, 
                            data = clusters_three_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_three_tests_aov_3)
```

### 3 Tests Cluster PWC - Test 3

```{r, include=FALSE}
cluster_three_tests_aov_pwc_3 <- clusters_three_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_3_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_three_tests_aov_pwc_3 <- cluster_three_tests_aov_pwc_3 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_three_tests_aov_pwc_3 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


Row {.tabset}
-----------------------------------------------------------------------

### 4 Tests Cluster Summary - Test 1

```{r, include=FALSE}

clusters_four_impact_test_1 <- four_impact_test %>% 
  select(c(1:20, contains("_post_injury_1"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3),
         age_post_injury_4 = as.character(age_post_injury_4))

clusters_four_impact_test_1 <- clusters_four_impact_test_1 %>% 
  select(c(1:20, 44:49))

clusters_four_impact_test_1_scaled <- map_df(clusters_four_impact_test_1, rescale01b)

clusters_four_impact_test_1_scaled <- bind_cols(clusters_four_impact_test_1, 
                                             clusters_four_impact_test_1_scaled)

clusters_four_impact_test_1_scaled <- clusters_four_impact_test_1_scaled %>% 
  select(-c(21:26))

clusters_four_impact_test_1_scaled[sapply(clusters_four_impact_test_1_scaled, is.character)] <- lapply(clusters_four_impact_test_1_scaled[sapply(clusters_four_impact_test_1_scaled, is.character)], as.factor)

str(clusters_four_impact_test_1_scaled)

clusters_four_impact_test_1_long <- clusters_four_impact_test_1_scaled %>% 
    pivot_longer(
    cols = c(22:27),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 

clusters_four_impact_test_2 <- four_impact_test %>% 
  select(c(1:20, contains("_post_injury_2"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3),
         age_post_injury_4 = as.character(age_post_injury_4))

clusters_four_impact_test_2 <- clusters_four_impact_test_2 %>% 
  select(c(1:20, 44:49))

clusters_four_impact_test_2_scaled <- map_df(clusters_four_impact_test_2, rescale01b)

clusters_four_impact_test_2_scaled <- bind_cols(clusters_four_impact_test_2, 
                                             clusters_four_impact_test_2_scaled)

clusters_four_impact_test_2_scaled <- clusters_four_impact_test_2_scaled %>% 
  select(-c(21:26))

clusters_four_impact_test_2_scaled[sapply(clusters_four_impact_test_2_scaled, is.character)] <- lapply(clusters_four_impact_test_2_scaled[sapply(clusters_four_impact_test_2_scaled, is.character)], as.factor)

str(clusters_four_impact_test_2_scaled)

clusters_four_impact_test_2_long <- clusters_four_impact_test_2_scaled %>% 
    pivot_longer(
    cols = c(22:27),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  ) 

clusters_four_impact_test_3 <- four_impact_test %>% 
  select(c(1:20, contains("_post_injury_3"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3),
         age_post_injury_4 = as.character(age_post_injury_4))

clusters_four_impact_test_3 <- clusters_four_impact_test_3 %>% 
  select(c(1:20, 44:49))

clusters_four_impact_test_3_scaled <- map_df(clusters_four_impact_test_3, rescale01b)

clusters_four_impact_test_3_scaled <- bind_cols(clusters_four_impact_test_3, 
                                             clusters_four_impact_test_3_scaled)

clusters_four_impact_test_3_scaled <- clusters_four_impact_test_3_scaled %>% 
  select(-c(21:26))

clusters_four_impact_test_3_scaled[sapply(clusters_four_impact_test_3_scaled, is.character)] <- lapply(clusters_four_impact_test_3_scaled[sapply(clusters_four_impact_test_3_scaled, is.character)], as.factor)

str(clusters_four_impact_test_3_scaled)

clusters_four_impact_test_3_long <- clusters_four_impact_test_3_scaled %>% 
    pivot_longer(
    cols = c(22:27),
    names_to = "symptom_cluster",
    values_to = "post_injury_3_score",
    names_pattern = "(.*)_score_post_injury_3"
  ) 


clusters_four_impact_test_4 <- four_impact_test %>% 
  select(c(1:20, contains("_post_injury_4"))) %>% 
    mutate(#X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3),
         age_post_injury_4 = as.character(age_post_injury_4))

clusters_four_impact_test_4 <- clusters_four_impact_test_4 %>% 
  select(c(1:20, 44:49))

clusters_four_impact_test_4_scaled <- map_df(clusters_four_impact_test_4, rescale01b)

clusters_four_impact_test_4_scaled <- bind_cols(clusters_four_impact_test_4, 
                                             clusters_four_impact_test_4_scaled)

clusters_four_impact_test_4_scaled <- clusters_four_impact_test_4_scaled %>% 
  select(-c(21:26))

clusters_four_impact_test_4_scaled[sapply(clusters_four_impact_test_4_scaled, is.character)] <- lapply(clusters_four_impact_test_4_scaled[sapply(clusters_four_impact_test_4_scaled, is.character)], as.factor)

str(clusters_four_impact_test_4_scaled)

clusters_four_impact_test_4_long <- clusters_four_impact_test_4_scaled %>% 
    pivot_longer(
    cols = c(22:27),
    names_to = "symptom_cluster",
    values_to = "post_injury_4_score",
    names_pattern = "(.*)_score_post_injury_4"
  ) 


clusters_four_impact_test_join_1 <- left_join(clusters_four_impact_test_1_long,
                                               clusters_four_impact_test_2_long)

clusters_four_impact_test_join_2 <- left_join(clusters_four_impact_test_join_1, 
                                                 clusters_four_impact_test_3_long)

clusters_four_impact_test_complete <- left_join(clusters_four_impact_test_join_2,
                                                clusters_four_impact_test_4_long)

str(clusters_four_impact_test_complete)
```


```{r, include=TRUE}
cluster_smry_tbl(clusters_four_impact_test_complete, post_injury_1_score)
```

### 4 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_four_impact_test_complete, 
                     symptom_cluster, post_injury_1_score))
```

### 4 Tests Cluster ANOVA - Test 1

```{r, include=FALSE}
cluster_four_tests_aov_1 <- aov(post_injury_1_score ~ symptom_cluster, 
                            data = clusters_four_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_four_tests_aov_1)
```

### 4 Tests Cluster PWC - Test 1

```{r, include=FALSE}
cluster_four_tests_aov_pwc_1 <- clusters_four_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_1_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_four_tests_aov_pwc_1 <- cluster_four_tests_aov_pwc_1 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_four_tests_aov_pwc_1 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


### 4 Tests Cluster Summary - Test 2

```{r, include=TRUE}
cluster_smry_tbl(clusters_four_impact_test_complete, post_injury_2_score)
```

### 4 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_four_impact_test_complete, 
                     symptom_cluster, post_injury_2_score))
```

### 4 Tests Cluster ANOVA - Test 2

```{r, include=FALSE}
cluster_four_tests_aov_2 <- aov(post_injury_2_score ~ symptom_cluster, 
                            data = clusters_four_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_four_tests_aov_2)
```

### 4 Tests Cluster PWC - Test 2

```{r, include=FALSE}
cluster_four_tests_aov_pwc_2 <- clusters_four_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_2_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_four_tests_aov_pwc_2 <- cluster_four_tests_aov_pwc_2 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_four_tests_aov_pwc_2 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


### 4 Tests Cluster Summary - Test 3

```{r, include=TRUE}
cluster_smry_tbl(clusters_four_impact_test_complete, post_injury_3_score)
```

### 4 Tests Cluster Boxplot - Test 3

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_four_impact_test_complete, 
                     symptom_cluster, post_injury_3_score))
```

### 4 Tests Cluster ANOVA - Test 3

```{r, include=FALSE}
cluster_four_tests_aov_3 <- aov(post_injury_3_score ~ symptom_cluster, 
                            data = clusters_four_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_four_tests_aov_3)
```

### 4 Tests Cluster PWC - Test 3

```{r, include=FALSE}
cluster_four_tests_aov_pwc_3 <- clusters_four_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_3_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_four_tests_aov_pwc_3 <- cluster_four_tests_aov_pwc_3 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_four_tests_aov_pwc_3 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


### 4 Tests Cluster Summary - Test 4

```{r, include=TRUE}
cluster_smry_tbl(clusters_four_impact_test_complete, post_injury_4_score)
```

### 4 Tests Cluster Boxplot - Test 4

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_four_impact_test_complete, 
                     symptom_cluster, post_injury_4_score))
```

### 4 Tests Cluster ANOVA - Test 4

```{r, include=FALSE}
cluster_four_tests_aov_4 <- aov(post_injury_4_score ~ symptom_cluster, 
                            data = clusters_four_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_four_tests_aov_4)
```

### 4 Tests Cluster PWC - Test 4

```{r, include=FALSE}
cluster_four_tests_aov_pwc_4 <- clusters_four_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_4_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_four_tests_aov_pwc_4 <- cluster_four_tests_aov_pwc_4 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_four_tests_aov_pwc_4 %>% 
  reactable(pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    searchable = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE)
```


# Gender-Cluster Interaction  

Sidebar {.sidebar}
------------

**Research Questions:**

  1. Does reported symptom severity differ by specific symptom cluster?
  2. Do males and females report differing levels of symptom severity?
  3. Are differences in symptom cluster severity ratings related to the differences between male and female symptom severity ratings?
  

**One Test Summary:**

  * Significant interaction effect between symptom cluster and gender identified
  * Non-significant PWC include:
    + Male sleep/male cognitive
    + Male ocular-motor/male anxiety-mood
    + Male headache migraine/female sleep
    + Male vestibular/female ocular-motor
    + Male headache-migraine/female cognitive 
    + Female vestibular/female anxiety-mood
    + Female sleep/female cognitive 
    + Sleep/cognitive
    + Ocular-motor/anxiety-mood 
  
**Two Tests Summary:**

_Test One:_

  * Significant interaction effect between symptom cluster and gender identified
  * Non-significant PWC include: 
    + Male sleep/male cognitive
    + Male ocular-motor/male anxiety-mood
    + Male sleep/female vestibular
    + Male cognitive/female vestibular
    + Male ocular-motor/female ocular-motor
    + Male vestibular/female ocular-motor
    + Male headache-migraine/female sleep
    + Male headache/female cognitive
    + Female sleep/female cognitive 
    + Male sleep/female anxiety-mood
    + Female vestibular/female anxiety-mood
    + Sleep/cognitive 
    
_Test Two:_

  * Significant interaction effect between symptom cluster and gender identified
  * Non-significant PWC include: 
    + Male vestibular/male ocular-motor
    + Male vestibular/male anxiety-mood
    + Male sleep/male cognitive 
    + Male ocular-motor/male anxiety-mood 
    + Male vestibular/female vestibular 
    + Male ocular-motor/female ocular-motor
    + Male vestibular/female ocular-motor
    + Male headache-migraine/female sleep
    + Male anxiety-mood/female ocular-motor 
    + Female vestibular/female ocular-motor
    + Male headache-migraine/female cognitive 
    + Female vestibular/female anxiety-mood 
    + Male cognitive/female anxiety-mood 
    + Male sleep/female anxiety-mood 
    + Female sleep/female cognitive 
    + Vestibular/anxiety-mood
    + Sleep/cognitive 
    

**Three Tests Summary:**

_Test One:_

  * Significant interaction effect between symptom cluster and gender identified
  * Non-significant PWC include: 
    + Male sleep/male cognitive 
    + Male ocular-motor/male anxiety-mood 
    + Male vestibular/female vestibular 
    + Male sleep/female vestibular 
    + Male ocular-motor/female ocular-motor
    + Male vestibular/female ocular-motor
    + Male cognitive/female sleep
    + Male headache-migraine/female sleep
    + Male anxiety-mood/female ocular-motor
    + Male headache-migraine/female cognitive 
    + Female vestibular/female anxiety-mood 
    + Male/cognitive/female anxiety-mood
    + Male sleep/female anxiety-mood 
    + Male vestibular/female anxiety-mood 
    + Female sleep/female cognitive 
    
_Test Two:_

  * Significant interaction effect between symptom cluster and gender identified
  * Non-significant PWC include: 
    + Male vestibular/male ocular-motor
    + Male vestibular/male anxiety-mood 
    + Male sleep/male cognitive 
    + Male ocular-motor/male anxiety-mood 
    + Male vestibular/female vestibular 
    + Male ocular-motor/female vestibular 
    + Male anxiety-mood/female vestibular
    + Male ocular-motor/female ocular-motor
    + Male vestibular/female ocular-motor
    + Male cognitive/female sleep
    + Male headache-migraine/female sleep
    + Male sleep/female sleep
    + Male anxiety-mood/female ocular-motor
    + Female vestibular/female ocular-motor 
    + Male cognitive/female cognitive 
    + Male headache-migraine/female cognitive 
    + Male sleep/female cognitive 
    + Female sleep/female cognitive 
    + Male vestibular/female anxiety-mood 
    + Male ocular-motor/female anxiety-mood 
    + Male cognitive/female anxiety-mood
    + Female vestibular/female anxiety-mood 
    + Female ocular-motor/female anxiety-mood
    + Vestibular/ocular-motor 
    + Ocular-motor/anxiety-mood
    + Vestibular/anxiety-mood
    + Sleep/cognitive 
    
    
_Test Three:_

  * Significant interaction effect between symptom cluster and gender identified
  * Non-significant PWC include: 
    + Male vestibular/male ocular-motor
    + Male vestibular/male anxiety-mood
    + Male headache-migraine/male cognitive 
    + Male sleep/male cognitive 
    + Male sleep/male headache-migraine 
    + Male ocular-motor/male anxiety-mood
    + Male vestibular/female vestibular
    + Male sleep/female vestibular
    + Male ocular-motor/female vestibular 
    + Male cognitive/female vestibular 
    + Male ocular-motor/female ocular-motor 
    + Male sleep/female ocular-motor
    + Male vestibular/female ocular-motor 
    + Male cognitive/female sleep
    + Male headache-migraine/female sleep
    + Male sleep/female sleep
    + Male anxiety-mood/female ocular-motor
    + Female vestibular/female ocular-motor
    + Female sleep/female headache-migraine 
    + Male sleep/female cognitive 
    + Male headache-migraine/female cognitive 
    + Male cognitive/female cognitive 
    + Female sleep/female cognitive 
    + Female headache-migraine/female cognitive 
    + Male sleep/female anxiety-mood 
    + Male headache-migraine/female anxiety-mood
    + Male cognitive/female anxiety-mood
    + Female vestibular/female anxiety-mood
    + Female sleep/female anxiety-mood
    + Female cognitive/female anxiety-mood
    + Vestibular/ocular-motor
    + Sleep/cognitive 
    + Headache-migraine/cognitive 
    + Vestibular/anxiety-mood
    + Ocular-motor/anxiety-mood 
    

**Four Tests Summary:**

_Test One:_

  * Interaction effect between symptom cluster and gender **not** significant 
  * Main effect of symptom cluster significant 
  * Main effect of gender significant 

    
_Test Two:_

  * Interaction effect between symptom cluster and gender **not** significant 
  * Main effect of symptom cluster significant 
  * Main effect of gender significant 
    
    
_Test Three:_

  * Interaction effect between symptom cluster and gender **not** significant 
  * Main effect of symptom cluster significant 
  * Main effect of gender significant 
  
_Test Four:_ 

  * Significant interaction effect between symptom cluster and gender identified
  * Many non-significant PWC
  * Significant PWC include: 
    + Male vestibular/female sleep
    + Male anxiety-mood/female sleep
    + Male anxiety-mood/female headache-migraine 
    + Male cognitive/female headache-migraine 
    + Male headache-migraine/female headache-migraine 
    + Male ocular-motor/female headache-migraine 
    + Male sleep/female headache-migraine 
    + Male vestibular/female headache-migraine 
    + Female vestibular/female headache-migraine 
    + Female ocular-motor/female headache-migraine 
    + Male vestibular/female cognitive 
    + Male sleep/female cognitive 
    + Male ocular-motor/female cognitive 
    + Male headache-migraine/female cognitive 
    + Male cognitive/female cognitive 
    + Male anxiety-mood/female cognitive 
    + Female vestibular/Female cognitive 
    + Female cognitive/female ocular-motor 
    + Female headache-migraine/female anxiety-mood 
    + Male/female 
    + Vestibular/headache-migraine 
    + Headache-migraine/anxiety-mood
    + Vestibular/cognitive 
    + Ocular-motor/headache-migraine 
    
    
    
    

Row {.tabset}
-----------------------------------------------------------------------

### 1 Test Cluster Summary 

```{r, include=FALSE}
clusters_one_impact_test_scaled_long


scaled_cluster_gender_tbl <- function(df, a, b, c) {
  df %>% 
    group_by({{a}}, {{b}}) %>% 
    summarize(Mean = mean({{c}}),
              SD = sd({{c}}),
              Min = min({{c}}),
              Max = max({{c}})) %>%  
    mutate(Mean = as.numeric(Mean),
           SD = as.numeric(SD),
           Min = as.numeric(Min),
           Max = as.numeric(Max)) %>% 
    arrange(desc(Mean)) %>% 
    mutate_if(is.numeric, round, 2) %>% 
    reactable(
      defaultColDef = colDef(align = "center"),
      columns = list(
        gender = colDef(name = "Gender"),
        symptom_cluster = colDef(name = "Symptom Cluster"),
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE))
      ),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )    
}

scaled_cluster_gender_tbl(clusters_one_impact_test_scaled_long,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)

```

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_one_impact_test_scaled_long,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### Non-Normalized Summary

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_one_impact_test_long,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```


### 1 Test Cluster Boxplot 

```{r, include=FALSE}

ggplot(clusters_one_impact_test_scaled_long,
  aes(symptom_cluster, post_injury_1_score, color = gender)) +
  geom_boxplot() + 
  scale_color_OkabeIto() + 
  coord_flip() +
  labs(x = "",
       y = "Scaled Severity Score") +
  theme(legend.title = element_blank())

gender_cluster_bxp <- function(df, a, b, c) {
ggplot(df,
  aes({{a}}, {{b}}, color = {{c}})) +
  geom_boxplot() + 
  scale_color_OkabeIto() + 
  coord_flip() +
  labs(x = "",
       y = "Scaled Severity Score") +
  theme(legend.title = element_blank()) 
}


gender_cluster_bxp(clusters_one_impact_test_scaled_long,
                   symptom_cluster,
                   post_injury_1_score,
                   gender)

```

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_one_impact_test_scaled_long,
                            symptom_cluster,
                            post_injury_1_score,
                            gender))
```


### 1 Test Cluster Two-Way ANOVA 

```{r, include=FALSE}
cluster_one_test_aov2 <- aov(post_injury_1_score ~ symptom_cluster * gender, 
                            data = clusters_one_impact_test_scaled_long)


library(car)

summary(cluster_one_test_aov2)

cluster_one_test_aov2

Anova(cluster_one_test_aov2, type = "III")

TukeyHSD(cluster_one_test_aov2)
TukeyHSD(cluster_one_test_aov2, which = "gender")
TukeyHSD(cluster_one_test_aov2, which = "symptom_cluster")


library(broom)

res_1 <- TukeyHSD(cluster_one_test_aov2)

tidy(res_1) %>% 
  reactable()

a_1 <- Anova(cluster_one_test_aov2, type = "III")

summary(a_1)

reactable(a_1)

anova_react <- function(df) {
  df %>% 
    reactable(
      defaultColDef = colDef(align = "center",
                             format = colFormat(digits = 2, separators = TRUE)),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE
    )
}

pwc_react <- function(df) {
  df %>% 
    reactable(
      defaultColDef = colDef(align = "center",
                             format = colFormat(digits = 3, separators = TRUE)),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )  
}

```

```{r, include=TRUE}
anova_react(a_1)
```

### 1 Test Cluster PWC

```{r, include=TRUE}
tidy(res_1) %>% 
  pwc_react()
```


Row {.tabset}
-----------------------------------------------------------------------

### 2 Tests Cluster Summary - Test 1

```{r, include=FALSE}
clusters_two_impact_test_complete[sapply(clusters_two_impact_test_complete, is.character)] <- lapply(clusters_two_impact_test_complete[sapply(clusters_two_impact_test_complete, is.character)], as.factor)

str(clusters_two_impact_test_complete)
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_two_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### Non-Normalized Summary

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_two_impact_test_1_long,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### 2 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_two_impact_test_complete,
                            symptom_cluster,
                            post_injury_1_score,
                            gender))
```

### 2 Tests Cluster Two-Way ANOVA - Test 1

```{r, include=FALSE}
cluster_two_tests_aov2_1 <- aov(post_injury_1_score ~ symptom_cluster * gender, 
                            data = clusters_two_impact_test_complete)


summary(cluster_two_tests_aov2_1)

cluster_two_tests_aov2_1

Anova(cluster_two_tests_aov2_1, type = "III")

TukeyHSD(cluster_two_tests_aov2_1)


res_2_1 <- TukeyHSD(cluster_two_tests_aov2_1)


a_2_1 <- Anova(cluster_two_tests_aov2_1, type = "III")

```


```{r, include=TRUE}
anova_react(a_2_1)
```

### 2 Tests Cluster PWC - Test 1

```{r, include=TRUE}
tidy(res_2_1) %>% 
  pwc_react()
```


### 2 Tests Cluster Summary - Test 2

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_two_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```

### 2 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_two_impact_test_complete,
                            symptom_cluster,
                            post_injury_2_score,
                            gender))
```

### 2 Tests Cluster Two-Way ANOVA - Test 2

```{r, include=FALSE}
cluster_two_tests_aov2_2 <- aov(post_injury_2_score ~ symptom_cluster * gender, 
                            data = clusters_two_impact_test_complete)


summary(cluster_two_tests_aov2_2)

cluster_two_tests_aov2_2

Anova(cluster_two_tests_aov2_2, type = "III")

TukeyHSD(cluster_two_tests_aov2_2)


res_2_2 <- TukeyHSD(cluster_two_tests_aov2_2)


a_2_2 <- Anova(cluster_two_tests_aov2_2, type = "III")

```


```{r, include=TRUE}
anova_react(a_2_2)
```

### 2 Tests Cluster PWC - Test 1

```{r, include=TRUE}
tidy(res_2_2) %>% 
  pwc_react()
```


Row {.tabset}
-----------------------------------------------------------------------

### 3 Tests Cluster Summary - Test 1

```{r, include=FALSE}
clusters_three_impact_test_complete[sapply(clusters_three_impact_test_complete, is.character)] <- lapply(clusters_three_impact_test_complete[sapply(clusters_three_impact_test_complete, is.character)], as.factor)

str(clusters_three_impact_test_complete)
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_three_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### 3 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_three_impact_test_complete,
                            symptom_cluster,
                            post_injury_1_score,
                            gender))
```

### 3 Tests Cluster Two-Way ANOVA - Test 1

```{r, include=FALSE}
cluster_three_tests_aov2_1 <- aov(post_injury_1_score ~ symptom_cluster * gender, 
                            data = clusters_three_impact_test_complete)


summary(cluster_three_tests_aov2_1)

cluster_three_tests_aov2_1

Anova(cluster_three_tests_aov2_1, type = "III")

TukeyHSD(cluster_three_tests_aov2_1)

res_3_1 <- TukeyHSD(cluster_three_tests_aov2_1)


a_3_1 <- Anova(cluster_three_tests_aov2_1, type = "III")

```


```{r, include=TRUE}
anova_react(a_3_1)
```

### 3 Tests Cluster PWC - Test 1

```{r, include=TRUE}
tidy(res_3_1) %>% 
  pwc_react()
```


### 3 Tests Cluster Summary - Test 2

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_three_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```

### 3 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_three_impact_test_complete,
                            symptom_cluster,
                            post_injury_2_score,
                            gender))
```

### 3 Tests Cluster Two-Way ANOVA - Test 2

```{r, include=FALSE}
cluster_three_tests_aov2_2 <- aov(post_injury_2_score ~ symptom_cluster * gender, 
                            data = clusters_three_impact_test_complete)


summary(cluster_three_tests_aov2_2)

cluster_three_tests_aov2_2

Anova(cluster_three_tests_aov2_2, type = "III")

TukeyHSD(cluster_three_tests_aov2_2)


res_3_2 <- TukeyHSD(cluster_three_tests_aov2_2)


a_3_2 <- Anova(cluster_three_tests_aov2_2, type = "III")

```


```{r, include=TRUE}
anova_react(a_3_2)
```

### 3 Tests Cluster PWC - Test 2

```{r, include=TRUE}
tidy(res_3_2) %>% 
  pwc_react()
```


### 3 Tests Cluster Summary - Test 3

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_three_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_3_score)
```

### 3 Tests Cluster Boxplot - Test 3

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_three_impact_test_complete,
                            symptom_cluster,
                            post_injury_3_score,
                            gender))
```

### 3 Tests Cluster Two-Way ANOVA - Test 3

```{r, include=FALSE}
cluster_three_tests_aov2_3 <- aov(post_injury_3_score ~ symptom_cluster * gender, 
                            data = clusters_three_impact_test_complete)


summary(cluster_three_tests_aov2_3)

cluster_three_tests_aov2_3

Anova(cluster_three_tests_aov2_3, type = "III")

TukeyHSD(cluster_three_tests_aov2_3)


res_3_3 <- TukeyHSD(cluster_three_tests_aov2_3)


a_3_3 <- Anova(cluster_three_tests_aov2_3, type = "III")

```


```{r, include=TRUE}
anova_react(a_3_3)
```

### 3 Tests Cluster PWC - Test 3

```{r, include=TRUE}
tidy(res_3_3) %>% 
  pwc_react()
```


Row {.tabset}
-----------------------------------------------------------------------

### 4 Tests Cluster Summary - Test 1

```{r, include=FALSE}
clusters_four_impact_test_complete[sapply(clusters_four_impact_test_complete, is.character)] <- lapply(clusters_four_impact_test_complete[sapply(clusters_four_impact_test_complete, is.character)], as.factor)

str(clusters_four_impact_test_complete)
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_four_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### 4 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_four_impact_test_complete,
                            symptom_cluster,
                            post_injury_1_score,
                            gender))
```

### 4 Tests Cluster Two-Way ANOVA - Test 1

```{r, include=FALSE}
cluster_four_tests_aov2_1 <- aov(post_injury_1_score ~ symptom_cluster * gender, 
                            data = clusters_four_impact_test_complete)


summary(cluster_four_tests_aov2_1)

cluster_four_tests_aov2_1

Anova(cluster_four_tests_aov2_1, type = "III")

TukeyHSD(cluster_four_tests_aov2_1)

res_4_1 <- TukeyHSD(cluster_four_tests_aov2_1)


a_4_1 <- Anova(cluster_four_tests_aov2_1, type = "III")


four_tests_main_effect_cluster_1 <- aov(post_injury_1_score ~ symptom_cluster,
                                      data = clusters_four_impact_test_complete)

summary(four_tests_main_effect_cluster_1)

four_tests_main_effect_gender_1 <- aov(post_injury_1_score ~ gender,
                                      data = clusters_four_impact_test_complete)

summary(four_tests_main_effect_gender_1)

cluster_main_effect_1 <- Anova(four_tests_main_effect_cluster_1)

summary(cluster_main_effect_1)

gender_main_effect_1 <- Anova(four_tests_main_effect_gender_1, type = "III")

summary(gender_main_effect_1)


cluster_4_1_pwc <- TukeyHSD(four_tests_main_effect_cluster_1)

gender_4_1_pwc <- TukeyHSD(four_tests_main_effect_gender_1)

```


```{r, include=TRUE}
anova_react(a_4_1)
```

### 4 Tests Cluster Interaction PWC - Test 1

```{r, include=TRUE}
tidy(res_4_1) %>% 
  pwc_react()
```

### 4 Tests - Test 1 One-Way ANOVA Main Effect Cluster

```{r, include=TRUE}
anova_react(cluster_main_effect_1)
```

### 4 Tests - Test 1 One-Way ANOVA Main Effect Cluster PWC

```{r, include=TRUE}
tidy(cluster_4_1_pwc) %>% 
  pwc_react()
```

### 4 Tests - Test 1 One-Way ANOVA Main Effect Gender

```{r, include=TRUE}
anova_react(gender_main_effect_1)
```

### 4 Tests - Test 1 One-Way ANOVA Main Effect Gender PWC

```{r, include=TRUE}
tidy(gender_4_1_pwc) %>% 
  pwc_react()
```



### 4 Tests Cluster Summary - Test 2

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_four_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```

### 4 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_four_impact_test_complete,
                            symptom_cluster,
                            post_injury_2_score,
                            gender))
```

### 4 Tests Cluster Two-Way ANOVA - Test 2

```{r, include=FALSE}
cluster_four_tests_aov2_2 <- aov(post_injury_2_score ~ symptom_cluster * gender, 
                            data = clusters_four_impact_test_complete)


summary(cluster_four_tests_aov2_2)

cluster_four_tests_aov2_2

Anova(cluster_four_tests_aov2_2, type = "III")

TukeyHSD(cluster_four_tests_aov2_2)


res_4_2 <- TukeyHSD(cluster_four_tests_aov2_2)


a_4_2 <- Anova(cluster_four_tests_aov2_2, type = "III")

```


```{r, include=TRUE}
anova_react(a_4_2)
```

### 4 Tests Cluster PWC - Test 2

```{r, include=TRUE}
tidy(res_4_2) %>% 
  pwc_react()
```


### 4 Tests Cluster Summary - Test 3

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_four_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_3_score)
```

### 4 Tests Cluster Boxplot - Test 3

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_four_impact_test_complete,
                            symptom_cluster,
                            post_injury_3_score,
                            gender))
```

### 4 Tests Cluster Two-Way ANOVA - Test 3

```{r, include=FALSE}
cluster_four_tests_aov2_3 <- aov(post_injury_3_score ~ symptom_cluster * gender, 
                            data = clusters_four_impact_test_complete)


summary(cluster_four_tests_aov2_3)

cluster_four_tests_aov2_3

Anova(cluster_four_tests_aov2_3, type = "III")

TukeyHSD(cluster_four_tests_aov2_3)


res_4_3 <- TukeyHSD(cluster_four_tests_aov2_3)


a_4_3 <- Anova(cluster_four_tests_aov2_3, type = "III")

```


```{r, include=TRUE}
anova_react(a_4_3)
```

### 4 Tests Cluster PWC - Test 3

```{r, include=TRUE}
tidy(res_4_3) %>% 
  pwc_react()
```


### 4 Tests Cluster Summary - Test 4

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_four_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_4_score)
```

### 4 Tests Cluster Boxplot - Test 4

```{r, include=TRUE}
ggplotly(gender_cluster_bxp(clusters_four_impact_test_complete,
                            symptom_cluster,
                            post_injury_4_score,
                            gender))
```

### 4 Tests Cluster Two-Way ANOVA - Test 4

```{r, include=FALSE}
cluster_four_tests_aov2_4 <- aov(post_injury_4_score ~ symptom_cluster * gender, 
                            data = clusters_four_impact_test_complete)


summary(cluster_four_tests_aov2_4)

cluster_four_tests_aov2_4

Anova(cluster_four_tests_aov2_4, type = "III")

TukeyHSD(cluster_four_tests_aov2_4)


res_4_4 <- TukeyHSD(cluster_four_tests_aov2_4)


a_4_4 <- Anova(cluster_four_tests_aov2_4, type = "III")

```


```{r, include=TRUE}
anova_react(a_4_4)
```

### 4 Tests Cluster PWC - Test 4

```{r, include=TRUE}
tidy(res_4_4) %>% 
  pwc_react()
```


# Test 1 Symptom Comparison 

Sidebar {.sidebar}
------------

  * Two-Way ANOVA between the number of tests completed and gender did not find a significant interaction effect for the overall, post-injury 1 total symptom severity score, but the main effects of number of tests completed and gender were both significant 
  * Follow up One-Way ANOVAs were completed on the main effects and were both significant
  * Females reported higher total symptom severity ratings than males at time of post-injury 1
  * All PWC between the number of tests completed were significant, indicating that individuals who end up taking more tests are more symptomatic than individuals who complete less tests. This should provide more support that symptom severity at time of first testing may correlate or predict recovery duration 
  

Row {.tabset}
-----------------------------------------------------------------------

### Total Symptom Score Summary 

```{r, include=FALSE}
names(one_impact_test)
names(two_impact_test)
names(three_impact_test)
names(four_impact_test)

test_one_comparison <- function(df) {
  df %>% 
  select(passport_id, 
         test_date_post_injury_1, 
         gender, 
         current_sport_post_injury_1, 
         total_symptom_score_post_injury_1,
         headache_migraine_cluster_score_post_injury_1,
         cognitive_cluster_score_post_injury_1,
         anxiety_mood_cluster_score_post_injury_1,
         ocular_motor_cluster_score_post_injury_1,
         vestibular_cluster_score_post_injury_1,
         sleep_cluster_score_post_injury_1)
}

one_impact_comparison <- test_one_comparison(one_impact_test)
two_impact_comparison <- test_one_comparison(two_impact_test)
three_impact_comparison <- test_one_comparison(three_impact_test)
four_impact_comparison <- test_one_comparison(four_impact_test)

all_test_one_comparison <- bind_rows(one_impact_comparison,
                                     two_impact_comparison,
                                     three_impact_comparison,
                                     four_impact_comparison,
                                     .id = "total_tests_completed")


str(all_test_one_comparison)

all_test_one_comparison <- all_test_one_comparison %>% 
  mutate(total_tests_completed = as.factor(total_tests_completed),
         gender = as.factor(gender))

str(all_test_one_comparison)

all_test_one_comparison_scaled <- map_df(all_test_one_comparison, rescale01b)


col_data <- all_test_one_comparison %>% 
  select(1:5)

all_test_one_comparison_scaled <- bind_cols(col_data, all_test_one_comparison_scaled)
  
str(all_test_one_comparison_scaled)
```


```{r, include=FALSE}
cluster_smry_tbl2 <- function(df, x) {
  df %>% 
  group_by(total_tests_completed, gender) %>% 
  summarize(mean = mean({{x}}),
            sd = sd({{x}}),
            min = min({{x}}),
            max = max({{x}}),
            total = length({{x}})) %>%
  mutate(mean = as.numeric(mean),
         sd = as.numeric(sd),
         min = as.numeric(min),
         max = as.numeric(max),
         total = as.numeric(total)) %>% 
  reactable(
    columns = list(
      total_tests_completed = colDef(name = "Total Tests Completed",
                            align = "center"),
      gender = colDef(name = "Gender",
                      align = "center"),
      mean = colDef(name = "Mean",
                    align = "center",
                    format = colFormat(digits = 2, separators = TRUE)),
      sd = colDef(name = "SD",
                  align = "center",
                  format = colFormat(digits = 2, separators = TRUE)),
      min = colDef(name = "Min",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      max = colDef(name = "Max",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      total = colDef(name = "Total",
                     align = "center",
                   format = colFormat(separators = TRUE, suffix = " concussions"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
  )
}

cluster_smry_tbl3 <- function(df, x) {
  df %>% 
  group_by(total_tests_completed) %>% 
  summarize(mean = mean({{x}}),
            sd = sd({{x}}),
            min = min({{x}}),
            max = max({{x}}),
            total = length({{x}})) %>%
  mutate(mean = as.numeric(mean),
         sd = as.numeric(sd),
         min = as.numeric(min),
         max = as.numeric(max),
         total = as.numeric(total)) %>% 
  reactable(
    columns = list(
      total_tests_completed = colDef(name = "Total Tests Completed",
                            align = "center"),
      mean = colDef(name = "Mean",
                    align = "center",
                    format = colFormat(digits = 2, separators = TRUE)),
      sd = colDef(name = "SD",
                  align = "center",
                  format = colFormat(digits = 2, separators = TRUE)),
      min = colDef(name = "Min",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      max = colDef(name = "Max",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      total = colDef(name = "Total",
                     align = "center",
                   format = colFormat(separators = TRUE, suffix = " concussions"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
  )
}

cluster_smry_tbl4 <- function(df, x) {
  df %>% 
  group_by(gender) %>% 
  summarize(mean = mean({{x}}),
            sd = sd({{x}}),
            min = min({{x}}),
            max = max({{x}}),
            total = length({{x}})) %>%
  mutate(mean = as.numeric(mean),
         sd = as.numeric(sd),
         min = as.numeric(min),
         max = as.numeric(max),
         total = as.numeric(total)) %>% 
  reactable(
    columns = list(
      gender = colDef(name = "Gender",
                      align = "center"),
      mean = colDef(name = "Mean",
                    align = "center",
                    format = colFormat(digits = 2, separators = TRUE)),
      sd = colDef(name = "SD",
                  align = "center",
                  format = colFormat(digits = 2, separators = TRUE)),
      min = colDef(name = "Min",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      max = colDef(name = "Max",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      total = colDef(name = "Total",
                     align = "center",
                   format = colFormat(separators = TRUE, suffix = " concussions"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
  )
}
```

```{r, include=TRUE}
cluster_smry_tbl2(all_test_one_comparison, total_symptom_score_post_injury_1)
```

### Total Symptom Score Summary - Gender

```{r, include=TRUE}
cluster_smry_tbl4(all_test_one_comparison, total_symptom_score_post_injury_1)
```

### Total Symptom Score Summary - Number of Tests Completed

```{r, include=TRUE}
cluster_smry_tbl3(all_test_one_comparison, total_symptom_score_post_injury_1)
```

### Total Symptom Score Boxplot 

```{r, include=FALSE}
test_1_bxp <- function(df, a, b, c) {
ggplot(df,
  aes({{a}}, {{b}}, color = {{c}})) +
  geom_boxplot() + 
  scale_color_OkabeIto() + 
  labs(x = "Number of Tests Completed",
       y = "Test 1 Severity Score") +
  theme(legend.title = element_blank()) 
}
```

```{r, include=TRUE}
ggplotly(test_1_bxp(all_test_one_comparison,
           total_tests_completed,
           total_symptom_score_post_injury_1,
           gender))
```

Row {.tabset}
-----------------------------------------------------------------------

### Total Symptom Score Two-Way ANOVA

```{r, include=FALSE}

total_symptom_score_aov <- aov(total_symptom_score_post_injury_1 ~ total_tests_completed * gender, data = all_test_one_comparison)


summary(total_symptom_score_aov)

total_symptom_score_aov_gender <- aov(total_symptom_score_post_injury_1 ~ gender,
                                      data = all_test_one_comparison)

summary(total_symptom_score_aov_gender)

total_symptom_score_aov_tests <- aov(total_symptom_score_post_injury_1 ~ total_tests_completed, data = all_test_one_comparison)

summary(total_symptom_score_aov_tests)

Anova(total_symptom_score_aov, type = "III")
Anova(total_symptom_score_aov_gender, type = "III")
Anova(total_symptom_score_aov_tests, type = "III")

TukeyHSD(total_symptom_score_aov)
TukeyHSD(total_symptom_score_aov_gender)
TukeyHSD(total_symptom_score_aov_tests)

total_symptom_interaction_results <- Anova(total_symptom_score_aov, type = "III")
total_symptom_gender_results <- Anova(total_symptom_score_aov_gender, type = "III")
total_symptom_tests_results <- Anova(total_symptom_score_aov_tests, type = "III")

total_pwc <- TukeyHSD(total_symptom_score_aov)
gender_pwc <- TukeyHSD(total_symptom_score_aov_gender)
tests_pwc <- TukeyHSD(total_symptom_score_aov_tests)

```


```{r, include=TRUE}
anova_react(total_symptom_interaction_results)
```

### Total Symptom Score Two-Way ANOVA Interaction PWC

```{r, include=TRUE}
tidy(total_pwc) %>% 
  pwc_react()
```

### Total Symptom Score One-Way ANOVA Main Effect Gender

```{r, include=TRUE}
anova_react(total_symptom_gender_results)
```

### Total Symptom Score One-Way ANOVA Main Effect Gender PWC

```{r, include=TRUE}
tidy(gender_pwc) %>% 
  pwc_react()
```

### Total Symptom Score One-Way ANOVA Main Effect Number of Tests Completed

```{r, include=TRUE}
anova_react(total_symptom_tests_results)
```

### Total Symptom Score One-Way ANOVA Main Effect Number of Tests Completed PWC

```{r, include=TRUE}
tidy(tests_pwc) %>% 
  pwc_react()
```

# Time 

Sidebar {.sidebar}
------------

This tab provides summary tables and plots for the duration of time between completion of ImPACT tests for the samples that completed two, three, or four tests. 

The number of days between tests was influential in the identification of data analyzed throughout this project. Following the results of Tamura et al. (2020) that identified the majority of students complete the RTP protocol within 30 days, the following filter restrictions were placed:

_Two Tests:_

  * Students who completed tests 1 and 2 within 30 days 
  
_Three Tests:_

  * Students who completed tests 1 and 2 within 30 days
  * Students who completed tests 2 and 3 within 30 days 
  * Students who completed tests 1 and 3 within 60 days 
  
_Four Tests:_

  * Students who completed tests 1 and 2 within 30 days
  * Students who completed tests 2 and 3 within 30 days 
  * Students who completed tests 3 and 4 within 30 days 
  * Students who completed tests 1 and 4 within 90 days 
  

Row {.tabset}
-----------------------------------------------------------------------

### Two Tests - Time Between Tests 1/2

```{r, include=FALSE}
names(two_impact_test)
names(three_impact_test)
names(four_impact_test)

time_react <- function(df, var) {
    df %>% 
      summarize(Mean = mean({{var}}),
                SD = sd({{var}}),
                Min = min({{var}}),
                Max = max({{var}}),
                Total = length({{var}})) %>% 
      mutate_if(is.numeric, round, 2) %>% 
      reactable(columns = list(
        Mean = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        SD = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Min = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Max = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " individuals"))
      ))
}

time_hist <- function(df, x) {
  ggplot(df, aes({{x}})) +
  geom_histogram(fill = "#56B4E9",
                color = "white", 
                alpha = 0.9,
                bins = 20) +
    labs(x = "Number of Days",
         y = "Number of Individuals")
}
```

```{r, include=TRUE}
time_react(two_impact_test, dys_btwn_tst_dts)
```

### Two Tests - Time Between Tests 1/2

```{r, include=TRUE}
ggplotly(time_hist(two_impact_test, dys_btwn_tst_dts))
```


Row {.tabset}
-----------------------------------------------------------------------

### Three Tests - Time Between Tests 1/2

```{r, include=TRUE}
time_react(three_impact_test, dys_btwn_tst_1_2)
```

### Three Tests - Time Between Tests 1/2

```{r, include=TRUE}
ggplotly(time_hist(three_impact_test, dys_btwn_tst_1_2))
```


### Three Tests - Time Between Tests 1/3

```{r, include=TRUE}
time_react(three_impact_test, dys_btwn_tst_1_3)
```

### Three Tests - Time Between Tests 1/3

```{r, include=TRUE}
ggplotly(time_hist(three_impact_test, dys_btwn_tst_1_3))
```

### Three Tests - Time Between Tests 2/3

```{r, include=TRUE}
time_react(three_impact_test, dys_btwn_tst_2_3)
```

### Three Tests - Time Between Tests 2/3

```{r, include=TRUE}
ggplotly(time_hist(three_impact_test, dys_btwn_tst_2_3))
```


Row {.tabset}
-----------------------------------------------------------------------

### Four Tests - Time Between Tests 1/2

```{r, include=TRUE}
time_react(four_impact_test, dys_btwn_tst_1_2)
```

### Four Tests - Time Between Tests 1/2

```{r, include=TRUE}
ggplotly(time_hist(four_impact_test, dys_btwn_tst_1_2))
```


### Four Tests - Time Between Tests 2/3

```{r, include=TRUE}
time_react(four_impact_test, dys_btwn_tst_2_3)
```

### Four Tests - Time Between Tests 2/3

```{r, include=TRUE}
ggplotly(time_hist(four_impact_test, dys_btwn_tst_2_3))
```

### Four Tests - Time Between Tests 3/4

```{r, include=TRUE}
time_react(four_impact_test, dys_btwn_tst_3_4)
```

### Four Tests - Time Between Tests 3/4

```{r, include=TRUE}
ggplotly(time_hist(four_impact_test, dys_btwn_tst_3_4))
```


### Four Tests - Time Between Tests 1/4

```{r, include=TRUE}
time_react(four_impact_test, dys_btwn_tst_1_4)
```

### Four Tests - Time Between Tests 1/4

```{r, include=TRUE}
ggplotly(time_hist(four_impact_test, dys_btwn_tst_1_4))
```


```{r, include=FALSE}
# create_react_time <- function(df, var) {
#     df %>% 
#       summarize(Mean = mean({{var}}),
#                 SD = sd({{var}}),
#                 Min = min({{var}}),
#                 Max = max({{var}}),
#                 Total = length({{var}})) %>% 
#       mutate_if(is.numeric, round, 2) %>% 
#       reactable(columns = list(
#         Mean = colDef(format = colFormat(separators = TRUE, suffix = " days")),
#         SD = colDef(format = colFormat(separators = TRUE, suffix = " days")),
#         Min = colDef(format = colFormat(separators = TRUE, suffix = " days")),
#         Max = colDef(format = colFormat(separators = TRUE, suffix = " days")),
#         Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
#       ))
# }
# 
# #join baseline dates with impact post injury tests 
# 
# str(baseline_dates)
# str(one_impact_test)
# 
# baseline_dates %>% 
#   count(passport_id)
# 
# bl_one_impact_test <- right_join(baseline_dates, one_impact_test)
# 
# bl_one_impact_test_2 <- left_join(baseline_dates, one_impact_test)
# 
# bl_one_impact_test_3 <- bind_cols(baseline_dates, one_impact_test)
```

# Rate

Sidebar {.sidebar}
------------

Row {.tabset}
-----------------------------------------------------------------------

### Two Tests Percent Decrease 

```{r, include=FALSE}
head(clusters_two_impact_test_1)

two_tests_cluster_scores_join <- left_join(clusters_two_impact_test_1,
                                           clusters_two_impact_test_2)

two_tests_cluster_scores_join <- two_tests_cluster_scores_join %>% 
  mutate(total_symptom_score_post_injury_1 = headache_migraine_cluster_score_post_injury_1 +
           cognitive_cluster_score_post_injury_1 +
           anxiety_mood_cluster_score_post_injury_1 +
           ocular_motor_cluster_score_post_injury_1 +
           vestibular_cluster_score_post_injury_1 +
           sleep_cluster_score_post_injury_1,
         total_symptom_score_post_injury_2 = headache_migraine_cluster_score_post_injury_2 +
           cognitive_cluster_score_post_injury_2 +
           anxiety_mood_cluster_score_post_injury_2 +
           ocular_motor_cluster_score_post_injury_2 +
           vestibular_cluster_score_post_injury_2 +
           sleep_cluster_score_post_injury_2)

two_tests_cluster_scores_means <- two_tests_cluster_scores_join %>% 
  select(c(13:26)) %>% 
  summarize(mean_headache_score_1 = mean(headache_migraine_cluster_score_post_injury_1),
            mean_headache_score_2 = mean(headache_migraine_cluster_score_post_injury_2),
            mean_cognitive_score_1 = mean(cognitive_cluster_score_post_injury_1),
            mean_cognitive_score_2 = mean(cognitive_cluster_score_post_injury_2),
            mean_anxiety_score_1 = mean(anxiety_mood_cluster_score_post_injury_1),
            mean_anxiety_score_2 = mean(anxiety_mood_cluster_score_post_injury_2),
            mean_ocular_motor_score_1 = mean(ocular_motor_cluster_score_post_injury_1),
            mean_ocular_motor_score_2 = mean(ocular_motor_cluster_score_post_injury_2),
            mean_vestibular_score_1 = mean(vestibular_cluster_score_post_injury_1),
            mean_vestibular_score_2 = mean(vestibular_cluster_score_post_injury_2),
            mean_sleep_score_1 = mean(sleep_cluster_score_post_injury_1),
            mean_sleep_score_2 = mean(sleep_cluster_score_post_injury_2),
            mean_total_score_1 = mean(total_symptom_score_post_injury_1),
            mean_total_score_2 = mean(total_symptom_score_post_injury_2)) %>% 
  mutate_if(is.numeric, ~round(., 2))

two_tests_cluster_scores_percent_decrease <- two_tests_cluster_scores_means %>% 
  mutate(perc_decrease_headache_score = (mean_headache_score_1 - mean_headache_score_2) / mean_headache_score_1 * 100,
         perc_decrease_cognitive_score = (mean_cognitive_score_1 - mean_cognitive_score_2) / mean_cognitive_score_1 * 100,
         perc_decrease_anxiety_score = (mean_anxiety_score_1 - mean_anxiety_score_2) / mean_anxiety_score_1 * 100,
         perc_decrease_ocular_motor_score = (mean_ocular_motor_score_1 - mean_ocular_motor_score_2) / mean_ocular_motor_score_1 * 100,
         perc_decrease_vestibular_score = (mean_vestibular_score_1 - mean_vestibular_score_2) / mean_vestibular_score_1 * 100,
         perc_decrease_sleep_score = (mean_sleep_score_1 - mean_sleep_score_2) / mean_sleep_score_1 * 100,
         perc_decrease_total_score = (mean_total_score_1 - mean_total_score_2) / mean_total_score_1 * 100) %>% 
  mutate_if(is.numeric, ~round(., 2))
  
two_tests_cluster <- c("Headache-Migraine",
                       "Cognitive",
                       "Anxiety-Mood",
                       "Ocular-Motor",
                       "Vestibular",
                       "Sleep",
                       "Total Symptom Score")

two_tests_test_one_mean <- c(3.02, 2.98, 1.91, 0.36, 2.01, 3.02, 13.31)
two_tests_test_two_mean <- c(0.94, 0.95, 0.56, 0.10, 0.45, 0.94, 3.94)
two_tests_percent_decrease <- c(.6887, .6812, .7068, .7222, .7761, .6887, .7040)

two_tests_percent_decrease_clean <- data_frame(two_tests_cluster,
                                               two_tests_test_one_mean,
                                               two_tests_test_two_mean,
                                               two_tests_percent_decrease)

str(two_tests_percent_decrease_clean)

two_tests_percent_decrease_tbl <- two_tests_percent_decrease_clean %>% 
  reactable(
    defaultColDef = colDef(align = "center"),
    columns = list(
      two_tests_cluster = colDef(name = "Symptom Cluster"),
      two_tests_test_one_mean = colDef(name = "Test One Mean"),
      two_tests_test_two_mean = colDef(name = "Test Two Mean"),
      two_tests_percent_decrease = colDef(name = "Percent Decrease",
                                          format = colFormat(percent = TRUE))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
  )
```

```{r, include=TRUE}
two_tests_percent_decrease_tbl
```

Row {.tabset}
-----------------------------------------------------------------------

### Three Tests Percent Decrease 

```{r, include=FALSE}
head(clusters_three_impact_test_1)
head(clusters_three_impact_test_2)
head(clusters_three_impact_test_3)

three_tests_cluster_scores_join_1 <- left_join(clusters_three_impact_test_1,
                                           clusters_three_impact_test_2)

three_tests_cluster_scores_join_all <- left_join(three_tests_cluster_scores_join_1,
                                                  clusters_three_impact_test_3)

three_tests_cluster_scores_join_all <- three_tests_cluster_scores_join_all %>% 
  mutate(total_symptom_score_post_injury_1 = headache_migraine_cluster_score_post_injury_1 +
           cognitive_cluster_score_post_injury_1 +
           anxiety_mood_cluster_score_post_injury_1 +
           ocular_motor_cluster_score_post_injury_1 +
           vestibular_cluster_score_post_injury_1 +
           sleep_cluster_score_post_injury_1,
         total_symptom_score_post_injury_2 = headache_migraine_cluster_score_post_injury_2 +
           cognitive_cluster_score_post_injury_2 +
           anxiety_mood_cluster_score_post_injury_2 +
           ocular_motor_cluster_score_post_injury_2 +
           vestibular_cluster_score_post_injury_2 +
           sleep_cluster_score_post_injury_2,
        total_symptom_score_post_injury_3 = headache_migraine_cluster_score_post_injury_3 +
           cognitive_cluster_score_post_injury_3 +
           anxiety_mood_cluster_score_post_injury_3 +
           ocular_motor_cluster_score_post_injury_3 +
           vestibular_cluster_score_post_injury_3 +
           sleep_cluster_score_post_injury_3)

three_tests_cluster_scores_means <- three_tests_cluster_scores_join_all %>% 
  select(c(17:37)) %>% 
  summarize(mean_headache_score_1 = mean(headache_migraine_cluster_score_post_injury_1),
            mean_headache_score_2 = mean(headache_migraine_cluster_score_post_injury_2),
            mean_headache_score_3 = mean(headache_migraine_cluster_score_post_injury_3),
            mean_cognitive_score_1 = mean(cognitive_cluster_score_post_injury_1),
            mean_cognitive_score_2 = mean(cognitive_cluster_score_post_injury_2),
            mean_cognitive_score_3 = mean(cognitive_cluster_score_post_injury_3),
            mean_anxiety_score_1 = mean(anxiety_mood_cluster_score_post_injury_1),
            mean_anxiety_score_2 = mean(anxiety_mood_cluster_score_post_injury_2),
            mean_anxiety_score_3 = mean(anxiety_mood_cluster_score_post_injury_3),
            mean_ocular_motor_score_1 = mean(ocular_motor_cluster_score_post_injury_1),
            mean_ocular_motor_score_2 = mean(ocular_motor_cluster_score_post_injury_2),
            mean_ocular_motor_score_3 = mean(ocular_motor_cluster_score_post_injury_3),
            mean_vestibular_score_1 = mean(vestibular_cluster_score_post_injury_1),
            mean_vestibular_score_2 = mean(vestibular_cluster_score_post_injury_2),
            mean_vestibular_score_3 = mean(vestibular_cluster_score_post_injury_3),
            mean_sleep_score_1 = mean(sleep_cluster_score_post_injury_1),
            mean_sleep_score_2 = mean(sleep_cluster_score_post_injury_2),
            mean_sleep_score_3 = mean(sleep_cluster_score_post_injury_3),
            mean_total_score_1 = mean(total_symptom_score_post_injury_1),
            mean_total_score_2 = mean(total_symptom_score_post_injury_2),
            mean_total_score_3 = mean(total_symptom_score_post_injury_3)) %>% 
  mutate_if(is.numeric, ~round(., 2))

three_tests_cluster_scores_percent_decrease <- three_tests_cluster_scores_means %>% 
  mutate(perc_decrease_headache_score_1_2 = (mean_headache_score_1 - mean_headache_score_2) / mean_headache_score_1 * 100,
         perc_decrease_headache_score_2_3 = (mean_headache_score_2 - mean_headache_score_3) / mean_headache_score_2 * 100,
         perc_decrease_headache_score_1_3 = (mean_headache_score_1 - mean_headache_score_3) / mean_headache_score_1 * 100,
         perc_decrease_cognitive_score_1_2 = (mean_cognitive_score_1 - mean_cognitive_score_2) / mean_cognitive_score_1 * 100,
         perc_decrease_cognitive_score_2_3 = (mean_cognitive_score_2 - mean_cognitive_score_3) / mean_cognitive_score_2 * 100,
         perc_decrease_cognitive_score_1_3 = (mean_cognitive_score_1 - mean_cognitive_score_3) / mean_cognitive_score_1 * 100,
         perc_decrease_anxiety_score_1_2 = (mean_anxiety_score_1 - mean_anxiety_score_2) / mean_anxiety_score_1 * 100,
         perc_decrease_anxiety_score_2_3 = (mean_anxiety_score_2 - mean_anxiety_score_3) / mean_anxiety_score_2 * 100,
         perc_decrease_anxiety_score_1_3 = (mean_anxiety_score_1 - mean_anxiety_score_3) / mean_anxiety_score_1 * 100,
         perc_decrease_ocular_motor_score_1_2 = (mean_ocular_motor_score_1 - mean_ocular_motor_score_2) / mean_ocular_motor_score_1 * 100,
         perc_decrease_ocular_motor_score_2_3 = (mean_ocular_motor_score_2 - mean_ocular_motor_score_3) / mean_ocular_motor_score_2 * 100,
         perc_decrease_ocular_motor_score_1_3 = (mean_ocular_motor_score_1 - mean_ocular_motor_score_3) / mean_ocular_motor_score_1 * 100,
         perc_decrease_vestibular_score_1_2 = (mean_vestibular_score_1 - mean_vestibular_score_2) / mean_vestibular_score_1 * 100,
         perc_decrease_vestibular_score_2_3 = (mean_vestibular_score_2 - mean_vestibular_score_3) / mean_vestibular_score_2 * 100,
         perc_decrease_vestibular_score_1_3 = (mean_vestibular_score_1 - mean_vestibular_score_3) / mean_vestibular_score_1 * 100,
         perc_decrease_sleep_score_1_2 = (mean_sleep_score_1 - mean_sleep_score_2) / mean_sleep_score_1 * 100,
         perc_decrease_sleep_score_2_3 = (mean_sleep_score_2 - mean_sleep_score_3) / mean_sleep_score_2 * 100,
         perc_decrease_sleep_score_1_3 = (mean_sleep_score_1 - mean_sleep_score_3) / mean_sleep_score_1 * 100,
         perc_decrease_total_score_1_2 = (mean_total_score_1 - mean_total_score_2) / mean_total_score_1 * 100,
         perc_decrease_total_score_2_3 = (mean_total_score_2 - mean_total_score_3) / mean_total_score_2 * 100,
         perc_decrease_total_score_1_3 = (mean_total_score_1 - mean_total_score_3) / mean_total_score_1 * 100) %>% 
  mutate_if(is.numeric, ~round(., 2))
  
three_tests_cluster <- c("Headache-Migraine",
                       "Cognitive",
                       "Anxiety-Mood",
                       "Ocular-Motor",
                       "Vestibular",
                       "Sleep",
                       "Total Symptom Score")

three_tests_test_one_mean <- c(3.55, 3.68, 2.32, 0.45, 2.48, 3.62, 16.09)
three_tests_test_two_mean <- c(1.34, 1.39, 0.76, 0.13, 0.68, 1.37, 5.68)
three_tests_test_three_mean <- c(0.57, 0.63, 0.39, 0.06, 0.26, 0.59, 2.49)
three_tests_percent_decrease_1_2 <- c(.6225, .6223, .6724, .7111, .7258, .6215, .6470)
three_tests_percent_decrease_2_3 <- c(.5746, .5468, .4868, .5385, .6176, .5693, .5616)
three_tests_percent_decrease_1_3 <- c(.8394, .8288, .8319, .8667, .8952, .8370, .8452)

three_tests_percent_decrease_clean <- data_frame(three_tests_cluster,
                                               three_tests_test_one_mean,
                                               three_tests_test_two_mean,
                                               three_tests_test_three_mean,
                                               three_tests_percent_decrease_1_2,
                                               three_tests_percent_decrease_2_3,
                                               three_tests_percent_decrease_1_3)


three_tests_percent_decrease_tbl <- three_tests_percent_decrease_clean %>% 
  reactable(
    defaultColDef = colDef(align = "center"),
    columns = list(
      three_tests_cluster = colDef(name = "Symptom Cluster"),
      three_tests_test_one_mean = colDef(name = "Test One Mean"),
      three_tests_test_two_mean = colDef(name = "Test Two Mean"),
      three_tests_test_three_mean = colDef(name = "Test Three Mean"),
      three_tests_percent_decrease_1_2 = colDef(name = "Tests 1 to 2 Percent Decrease",
                                          format = colFormat(percent = TRUE)),
      three_tests_percent_decrease_2_3 = colDef(name = "Tests 2 to 3 Percent Decrease",
                                          format = colFormat(percent = TRUE)),
      three_tests_percent_decrease_1_3 = colDef(name = "Tests 1 to 3 Percent Decrease",
                                          format = colFormat(percent = TRUE))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
  )
```

```{r, include=TRUE}
three_tests_percent_decrease_tbl
```

Row {.tabset}
-----------------------------------------------------------------------

### Four Tests Percent Decrease 

```{r, include=FALSE}
head(clusters_four_impact_test_1)
head(clusters_four_impact_test_2)
head(clusters_four_impact_test_3)
head(clusters_four_impact_test_4)

four_tests_cluster_scores_join_1 <- left_join(clusters_four_impact_test_1,
                                           clusters_four_impact_test_2)

four_tests_cluster_scores_join_2 <- left_join(four_tests_cluster_scores_join_1,
                                           clusters_four_impact_test_3)

four_tests_cluster_scores_join_all <- left_join(four_tests_cluster_scores_join_2,
                                                  clusters_four_impact_test_4)

four_tests_cluster_scores_join_all <- four_tests_cluster_scores_join_all %>% 
  mutate(total_symptom_score_post_injury_1 = headache_migraine_cluster_score_post_injury_1 +
           cognitive_cluster_score_post_injury_1 +
           anxiety_mood_cluster_score_post_injury_1 +
           ocular_motor_cluster_score_post_injury_1 +
           vestibular_cluster_score_post_injury_1 +
           sleep_cluster_score_post_injury_1,
         total_symptom_score_post_injury_2 = headache_migraine_cluster_score_post_injury_2 +
           cognitive_cluster_score_post_injury_2 +
           anxiety_mood_cluster_score_post_injury_2 +
           ocular_motor_cluster_score_post_injury_2 +
           vestibular_cluster_score_post_injury_2 +
           sleep_cluster_score_post_injury_2,
        total_symptom_score_post_injury_3 = headache_migraine_cluster_score_post_injury_3 +
           cognitive_cluster_score_post_injury_3 +
           anxiety_mood_cluster_score_post_injury_3 +
           ocular_motor_cluster_score_post_injury_3 +
           vestibular_cluster_score_post_injury_3 +
           sleep_cluster_score_post_injury_3,
        total_symptom_score_post_injury_4 = headache_migraine_cluster_score_post_injury_4 +
           cognitive_cluster_score_post_injury_4 +
           anxiety_mood_cluster_score_post_injury_4 +
           ocular_motor_cluster_score_post_injury_4 +
           vestibular_cluster_score_post_injury_4 +
           sleep_cluster_score_post_injury_4)

four_tests_cluster_scores_means <- four_tests_cluster_scores_join_all %>% 
  select(c(21:48)) %>% 
  summarize(mean_headache_score_1 = mean(headache_migraine_cluster_score_post_injury_1),
            mean_headache_score_2 = mean(headache_migraine_cluster_score_post_injury_2),
            mean_headache_score_3 = mean(headache_migraine_cluster_score_post_injury_3),
            mean_headache_score_4 = mean(headache_migraine_cluster_score_post_injury_4),
            mean_cognitive_score_1 = mean(cognitive_cluster_score_post_injury_1),
            mean_cognitive_score_2 = mean(cognitive_cluster_score_post_injury_2),
            mean_cognitive_score_3 = mean(cognitive_cluster_score_post_injury_3),
            mean_cognitive_score_4 = mean(cognitive_cluster_score_post_injury_4),
            mean_anxiety_score_1 = mean(anxiety_mood_cluster_score_post_injury_1),
            mean_anxiety_score_2 = mean(anxiety_mood_cluster_score_post_injury_2),
            mean_anxiety_score_3 = mean(anxiety_mood_cluster_score_post_injury_3),
            mean_anxiety_score_4 = mean(anxiety_mood_cluster_score_post_injury_4),
            mean_ocular_motor_score_1 = mean(ocular_motor_cluster_score_post_injury_1),
            mean_ocular_motor_score_2 = mean(ocular_motor_cluster_score_post_injury_2),
            mean_ocular_motor_score_3 = mean(ocular_motor_cluster_score_post_injury_3),
            mean_ocular_motor_score_4 = mean(ocular_motor_cluster_score_post_injury_4),
            mean_vestibular_score_1 = mean(vestibular_cluster_score_post_injury_1),
            mean_vestibular_score_2 = mean(vestibular_cluster_score_post_injury_2),
            mean_vestibular_score_3 = mean(vestibular_cluster_score_post_injury_3),
            mean_vestibular_score_4 = mean(vestibular_cluster_score_post_injury_4),
            mean_sleep_score_1 = mean(sleep_cluster_score_post_injury_1),
            mean_sleep_score_2 = mean(sleep_cluster_score_post_injury_2),
            mean_sleep_score_3 = mean(sleep_cluster_score_post_injury_3),
            mean_sleep_score_4 = mean(sleep_cluster_score_post_injury_4),
            mean_total_score_1 = mean(total_symptom_score_post_injury_1),
            mean_total_score_2 = mean(total_symptom_score_post_injury_2),
            mean_total_score_3 = mean(total_symptom_score_post_injury_3),
            mean_total_score_4 = mean(total_symptom_score_post_injury_4)) %>% 
  mutate_if(is.numeric, ~round(., 2))

four_tests_cluster_scores_percent_decrease <- four_tests_cluster_scores_means %>% 
  mutate(perc_decrease_headache_score_1_2 = (mean_headache_score_1 - mean_headache_score_2) / mean_headache_score_1 * 100,
         perc_decrease_headache_score_2_3 = (mean_headache_score_2 - mean_headache_score_3) / mean_headache_score_2 * 100,
         perc_decrease_headache_score_3_4 = (mean_headache_score_3 - mean_headache_score_4) / mean_headache_score_3 * 100,
         perc_decrease_headache_score_1_4 = (mean_headache_score_1 - mean_headache_score_4) / mean_headache_score_1 * 100,
         perc_decrease_cognitive_score_1_2 = (mean_cognitive_score_1 - mean_cognitive_score_2) / mean_cognitive_score_1 * 100,
         perc_decrease_cognitive_score_2_3 = (mean_cognitive_score_2 - mean_cognitive_score_3) / mean_cognitive_score_2 * 100,
         perc_decrease_cognitive_score_3_4 = (mean_cognitive_score_3 - mean_cognitive_score_4) / mean_cognitive_score_3 * 100,
         perc_decrease_cognitive_score_1_4 = (mean_cognitive_score_1 - mean_cognitive_score_4) / mean_cognitive_score_1 * 100,
         perc_decrease_anxiety_score_1_2 = (mean_anxiety_score_1 - mean_anxiety_score_2) / mean_anxiety_score_1 * 100,
         perc_decrease_anxiety_score_2_3 = (mean_anxiety_score_2 - mean_anxiety_score_3) / mean_anxiety_score_2 * 100,
         perc_decrease_anxiety_score_3_4 = (mean_anxiety_score_3 - mean_anxiety_score_4) / mean_anxiety_score_3 * 100,
         perc_decrease_anxiety_score_1_4 = (mean_anxiety_score_1 - mean_anxiety_score_4) / mean_anxiety_score_1 * 100,
         perc_decrease_ocular_motor_score_1_2 = (mean_ocular_motor_score_1 - mean_ocular_motor_score_2) / mean_ocular_motor_score_1 * 100,
         perc_decrease_ocular_motor_score_2_3 = (mean_ocular_motor_score_2 - mean_ocular_motor_score_3) / mean_ocular_motor_score_2 * 100,
         perc_decrease_ocular_motor_score_3_4 = (mean_ocular_motor_score_3 - mean_ocular_motor_score_4) / mean_ocular_motor_score_3 * 100,
         perc_decrease_ocular_motor_score_1_4 = (mean_ocular_motor_score_1 - mean_ocular_motor_score_4) / mean_ocular_motor_score_1 * 100,
         perc_decrease_vestibular_score_1_2 = (mean_vestibular_score_1 - mean_vestibular_score_2) / mean_vestibular_score_1 * 100,
         perc_decrease_vestibular_score_2_3 = (mean_vestibular_score_2 - mean_vestibular_score_3) / mean_vestibular_score_2 * 100,
         perc_decrease_vestibular_score_3_4 = (mean_vestibular_score_3 - mean_vestibular_score_4) / mean_vestibular_score_3 * 100,
         perc_decrease_vestibular_score_1_4 = (mean_vestibular_score_1 - mean_vestibular_score_4) / mean_vestibular_score_1 * 100,
         perc_decrease_sleep_score_1_2 = (mean_sleep_score_1 - mean_sleep_score_2) / mean_sleep_score_1 * 100,
         perc_decrease_sleep_score_2_3 = (mean_sleep_score_2 - mean_sleep_score_3) / mean_sleep_score_2 * 100,
         perc_decrease_sleep_score_3_4 = (mean_sleep_score_3 - mean_sleep_score_4) / mean_sleep_score_3 * 100,
         perc_decrease_sleep_score_1_4 = (mean_sleep_score_1 - mean_sleep_score_4) / mean_sleep_score_1 * 100,
         perc_decrease_total_score_1_2 = (mean_total_score_1 - mean_total_score_2) / mean_total_score_1 * 100,
         perc_decrease_total_score_2_3 = (mean_total_score_2 - mean_total_score_3) / mean_total_score_2 * 100,
         perc_decrease_total_score_3_4 = (mean_total_score_3 - mean_total_score_4) / mean_total_score_3 * 100,
         perc_decrease_total_score_1_4 = (mean_total_score_1 - mean_total_score_4) / mean_total_score_1 * 100) %>% 
  mutate_if(is.numeric, ~round(., 2))
  
four_tests_cluster <- c("Headache-Migraine",
                       "Cognitive",
                       "Anxiety-Mood",
                       "Ocular-Motor",
                       "Vestibular",
                       "Sleep",
                       "Total Symptom Score")

four_tests_test_one_mean <- c(4.04, 4.31, 2.70, 0.56, 3.06, 4.28, 18.95)
four_tests_test_two_mean <- c(1.83, 2.08, 1.07, 0.18, 1.03, 1.98, 8.17)
four_tests_test_three_mean <- c(0.99, 1.12, 0.63, 0.09, 0.48, 0.98, 4.28)
four_tests_test_four_mean <- c(0.60, 0.68, 0.43, 0.07, 0.29, 0.54, 2.62)
four_tests_percent_decrease_1_2 <- c(.5470, .5174, .6037, .6786, .6634, .5374, .5689)
four_tests_percent_decrease_2_3 <- c(.4590, .4615, .4112, .5000, .5340, .5051, .4761)
four_tests_percent_decrease_3_4 <- c(.3939, .3929, .3175, .2222, .3958, .4490, .3879)
four_tests_percent_decrease_1_4 <- c(.8515, .8422, .8407, .8750, .9052, .8738, .8617)

four_tests_percent_decrease_clean <- data_frame(four_tests_cluster,
                                               four_tests_test_one_mean,
                                               four_tests_test_two_mean,
                                               four_tests_test_three_mean,
                                               four_tests_test_four_mean,
                                               four_tests_percent_decrease_1_2,
                                               four_tests_percent_decrease_2_3,
                                               four_tests_percent_decrease_3_4,
                                               four_tests_percent_decrease_1_4)


four_tests_percent_decrease_tbl <- four_tests_percent_decrease_clean %>% 
  reactable(
    defaultColDef = colDef(align = "center"),
    columns = list(
      four_tests_cluster = colDef(name = "Symptom Cluster"),
      four_tests_test_one_mean = colDef(name = "Test One Mean"),
      four_tests_test_two_mean = colDef(name = "Test Two Mean"),
      four_tests_test_three_mean = colDef(name = "Test Three Mean"),
      four_tests_test_four_mean = colDef(name = "Test Four Mean"),
      four_tests_percent_decrease_1_2 = colDef(name = "Tests 1 to 2 Percent Decrease",
                                          format = colFormat(percent = TRUE)),
      four_tests_percent_decrease_2_3 = colDef(name = "Tests 2 to 3 Percent Decrease",
                                          format = colFormat(percent = TRUE)),
      four_tests_percent_decrease_3_4 = colDef(name = "Tests 3 to 4 Percent Decrease",
                                          format = colFormat(percent = TRUE)),
      four_tests_percent_decrease_1_4 = colDef(name = "Tests 1 to 4 Percent Decrease",
                                          format = colFormat(percent = TRUE))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
  )
```

```{r, include=TRUE}
four_tests_percent_decrease_tbl
```

Row {.tabset}
-----------------------------------------------------------------------

### Two Tests Non-Normalized Test 1 Summary 

```{r, include=FALSE}
clusters_two_impact_test_1

two_clusters_test_1 <- clusters_two_impact_test_1 %>% 
  pivot_longer(
    cols = c(13:18),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(two_clusters_test_1,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### Two Tests Non-Normalized Test 2 Summary 

```{r, include=FALSE}

two_clusters_test_2 <- clusters_two_impact_test_2 %>% 
  pivot_longer(
    cols = c(13:18),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(two_clusters_test_2,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```


Row {.tabset}
-----------------------------------------------------------------------

### Three Tests Non-Normalized Test 1 Summary 

```{r, include=FALSE}

three_clusters_test_1 <- clusters_three_impact_test_1 %>% 
  pivot_longer(
    cols = c(17:22),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(three_clusters_test_1,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### Three Tests Non-Normalized Test 2 Summary 

```{r, include=FALSE}

three_clusters_test_2 <- clusters_three_impact_test_2 %>% 
  pivot_longer(
    cols = c(17:22),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(three_clusters_test_2,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```

### Three Tests Non-Normalized Test 3 Summary 

```{r, include=FALSE}

three_clusters_test_3 <- clusters_three_impact_test_3 %>% 
  pivot_longer(
    cols = c(17:22),
    names_to = "symptom_cluster",
    values_to = "post_injury_3_score",
    names_pattern = "(.*)_score_post_injury_3"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(three_clusters_test_3,
                          gender,
                          symptom_cluster,
                          post_injury_3_score)
```


Row {.tabset}
-----------------------------------------------------------------------

### Four Tests Non-Normalized Test 1 Summary 

```{r, include=FALSE}



four_clusters_test_1 <- clusters_four_impact_test_1 %>% 
  pivot_longer(
    cols = c(21:26),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(four_clusters_test_1,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```


### Four Tests Non-Normalized Test 2 Summary 

```{r, include=FALSE}
four_clusters_test_2 <- clusters_four_impact_test_2 %>% 
  pivot_longer(
    cols = c(21:26),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(four_clusters_test_2,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```

### Four Tests Non-Normalized Test 3 Summary 

```{r, include=FALSE}
four_clusters_test_3 <- clusters_four_impact_test_3 %>% 
  pivot_longer(
    cols = c(21:26),
    names_to = "symptom_cluster",
    values_to = "post_injury_3_score",
    names_pattern = "(.*)_score_post_injury_3"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(four_clusters_test_3,
                          gender,
                          symptom_cluster,
                          post_injury_3_score)
```

### Four Tests Non-Normalized Test 4 Summary 

```{r, include=FALSE}
four_clusters_test_4 <- clusters_four_impact_test_4 %>% 
  pivot_longer(
    cols = c(21:26),
    names_to = "symptom_cluster",
    values_to = "post_injury_4_score",
    names_pattern = "(.*)_score_post_injury_4"
  )
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(four_clusters_test_4,
                          gender,
                          symptom_cluster,
                          post_injury_4_score)
```